jwwceo Posted November 7, 2007 Share Posted November 7, 2007 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 More sharing options...
obsidian Posted November 7, 2007 Share Posted November 7, 2007 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 More sharing options...
jwwceo Posted November 7, 2007 Author Share Posted November 7, 2007 Thank you! Works great!!!! Link to comment https://forums.phpfreaks.com/topic/76427-solved-javascript/#findComment-387065 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.