2
   

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

 
 
Post: # 507,461
View Profile chevalier
 
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]
 
Post: # 520,514
View Profile 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
 
Post: # 520,574
View Profile 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
 
Post: # 522,051
View Profile 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
 
Post: # 523,272
View Profile 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

THANK YOU CRAVEN AND NICK!!! - Discussion by dagmaraka
I'm the developer - Discussion by Nick Ashley
The version 10 bug. Worse then Y2K! - Discussion by Nick Ashley
how to earn money in internet - Discussion by rizwanaraj
There is no Wisdom in Crowds - Discussion by ebrown p
CSS Border style colors - Question by meesa
Parallel Processing in PHP - Discussion by alirizwan
 
  1. able2know
  2. » How to address a specific <td> as an object in JavaScript?
Copyright © 2009 Horizontal Verticals :: Page generated in 0.35 seconds on 11/27/2009 at 03:35:34 Top End