coderb Posted May 15, 2008 Share Posted May 15, 2008 Hi All, This is really killing me, one of those, no doubt, simple-to-fix-but-I-just-cannot-see-it problems. just got this test page, on page load the script should change the bg color of the element tried it with td and div, but both return error 'Object Required' - but why?? <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Edit Sales People - OFFICE</title> <link rel="stylesheet" type="text/css" href="../include/style.css"> <script type="text/javascript"> document.getElementById("test").style.backgroundColor = "#FFFFCC"; </script> </head> <body> <table> <tr> <td class="Light">Role</td> <td id="x">hello</td> </tr> </table> <div id="test">hellow</div> </body> </html> Quote Link to comment Share on other sites More sharing options...
RichardRotterdam Posted May 15, 2008 Share Posted May 15, 2008 its because the script doesnt see the test element yet the DOM hasnt fully loaded. if you would place it under the test element it should work <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Edit Sales People - OFFICE</title> <link rel="stylesheet" type="text/css" href="../include/style.css"> </head> <body> <table> <tr> <td class="Light">Role</td> <td id="x">hello</td> </tr> </table> <script type="text/javascript"> document.getElementById("test").style.backgroundColor = "#FFFFCC"; </script> <div id="test">hellow</div> </body> Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted May 16, 2008 Share Posted May 16, 2008 its because the script doesnt see the test element yet the DOM hasnt fully loaded. if you would place it under the test element it should work <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Edit Sales People - OFFICE</title> <link rel="stylesheet" type="text/css" href="../include/style.css"> </head> <body> <table> <tr> <td class="Light">Role</td> <td id="x">hello</td> </tr> </table> <script type="text/javascript"> document.getElementById("test").style.backgroundColor = "#FFFFCC"; </script> <div id="test">hellow</div> </body> Eewww, putting JS somewhere other than the head is icky. ;-) If speed isn't a factor, why not wait for the DOM to load? <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Edit Sales People - OFFICE</title> <link rel="stylesheet" type="text/css" href="../include/style.css"> <script type="text/javascript"> window.onload = function() { document.getElementById("test").style.backgroundColor = "#FFFFCC"; } </script> </head> <body> <table> <tr> <td class="Light">Role</td> <td id="x">hello</td> </tr> </table> <div id="test">hellow</div> </body> </html> Quote Link to comment Share on other sites More sharing options...
coderb Posted May 27, 2008 Author Share Posted May 27, 2008 got side tracked, thanks for the help, and sorry for the delayed response. 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.