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 Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
jwwceo Posted November 7, 2007 Author Share Posted November 7, 2007 Thank you! Works great!!!! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.