Jump to content

[SOLVED] Javascript


jwwceo

Recommended Posts

Hello,

 

I am having trouble with using Javascript to change the background color of a TD. This is supposed to happen when a user changes a pulldown...

 

here is the relevant javascript code:

 

window.document.jwv.bgColor = "#f3f3f3";

 

and the HTML

 

<td name="jwv" valign="bottom" width="200" height="162">
<img id="product_thumbnail">
</td>

 

I get an error saying that jwv has no properties.

 

The TD is rendered before the Javascript runs.

 

Any ideas??

 

James

Link to comment
https://forums.phpfreaks.com/topic/76427-solved-javascript/
Share on other sites

You would be much better off using id attributes since name is not a valid TD attribute. Try this instead:

 

HTML:

<td id="jwv" valign="bottom" width="200" height="162">
<img id="product_thumbnail" />
</td>

 

Javascript:

document.getElementById('jwv').style.backgroundColor = '#f3f3f3';

 

If you have multiple cells that you are wanting to change, you'll want to assign them all a class attribute instead of id, and then you just loop over them like this:

eles = document.getElementsByTagname('TD');
for (var i = 0; i < eles.length; i++)
{
  if (eles[i].className == 'myclassname')
  {
    eles[i].style.backgroundColor = '#f3f3f3';
  }
}

 

Hope this helps.

Link to comment
https://forums.phpfreaks.com/topic/76427-solved-javascript/#findComment-387024
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.