1
   

How to address a specific <td> as an object in JavaScript?

 
 
Reply Sun 4 Jan, 2004 10:08 am
I'm having some problems with changing the background of table cells. I've tried the document.tables.td thing, but it didn't work. Document.all didn't work either. "ID" & getElementById also fails. What else could be done?

In <head>:
[code]<style>

#lefttop2 { background-repeat: no-repeat; background-position: center center; }
#top { background-repeat: no-repeat; background-position: center center; }
#righttop2 { background-repeat: no-repeat; background-position: center center; }
#leftside { background-repeat: no-repeat; background-position: center center; }
#rightside { background-repeat: no-repeat; background-position: center center; }
#leftbottom2 { background-repeat: no-repeat; background-position: center center; }
#bottom2 { background-repeat: no-repeat; background-position: center center; }
#rightbottom2 { background-repeat: no-repeat; background-position: center center; }
</style>


<script language="JavaScript">
<!--

var preimages = new Array("images/lefttop2.JPG","images/top.JPG","images/righttop2.JPG", "images/leftside.JPG","images/rightside.JPG","images/leftbottom2.JPG","images/bottom.JPG","images/rightbottom2.JPG");

var preimages2 = new Array();

for (i=0; i<preimages.length; i++) {

preimages2[i] = new Image()
preimages2[i].src = preimages[i] }

function showborder() {

document.getElementById("lefttop2").style.backgroundImage="url(" + preimages2[0].src + ")";
document.getElementById("top2").style.backgroundImage="url(" + preimages2[1].src + ")";
document.getElementById("rightttop2").style.backgroundImage="url(" + preimages2[2].src + ")";
document.getElementById("leftside").style.backgroundImage="url(" + preimages2[3].src + ")";
document.getElementById("rightside").style.backgroundImage="url(" + preimages2[4].src + ")";
document.getElementById("leftbottom2").style.backgroundImage="url(" + preimages2[5].src + ")";
document.getElementById("bottom2").style.backgroundImage="url(" + preimages2[6].src + ")";
document.getElementById("rightbottom2").style.backgroundImage="url(" + preimages2[7].src + ")"; }
//-->
</script>[/code]
  • Topic Stats
  • Top Replies
  • Link to this Topic
Type: Discussion • Score: 1 • Views: 2,972 • Replies: 4
No top replies

 
Glorius
 
  1  
Reply Wed 14 Jan, 2004 05:04 am
Check this sample.
Paste this html:
Code:
<table>
<tr>
<td id="cell1">cell</td>
</tr>
</table>


For changing background color of cell1 you can use one this two methods:

Direct access to bgColor property of TD
Code:document.all.cell1.bgColor = 'green'


Or using style collection:
Code:document.all.cell1.style.backgroundColor = 'red'


I prefer usinig first method because of browser incompatibility accessing to style collection
0 Replies
 
chevalier
 
  1  
Reply Wed 14 Jan, 2004 06:44 am
thanks
Thanks, but this one has always worked. I can't, however, change background pictures that way even through commands copied directly from reference and hints, for some reason. Does it mean the table has to have its name too, not only the cells. Or what can it be?
0 Replies
 
Glorius
 
  1  
Reply Thu 15 Jan, 2004 01:41 am
For changing background image (not color, sorry, I have't read you post carefully) you can do this:
Code:document.all.cell1.background = 'img1.jpg';
0 Replies
 
chevalier
 
  1  
Reply Thu 15 Jan, 2004 08:30 pm
Thanks. So it seems my code was right and a friend's site where I was trying to implement it was otherwise screwed. Good to know this at least Wink
0 Replies
 
 

Related Topics

Webdevelopment and hosting - Question by harisit2005
Showing an Ico File - Discussion by Brandon9000
how to earn money in internet - Discussion by rizwanaraj
The version 10 bug. Worse then Y2K! - Discussion by Nick Ashley
CSS Border style colors - Question by meesa
There is no Wisdom in Crowds - Discussion by ebrown p
THANK YOU CRAVEN AND NICK!!! - Discussion by dagmaraka
I'm the developer - Discussion by Nick Ashley
 
  1. Forums
  2. » How to address a specific <td> as an object in JavaScript?
Copyright © 2025 MadLab, LLC :: Terms of Service :: Privacy Policy :: Page generated in 0.03 seconds on 01/16/2025 at 07:19:27