Jump to content

Object Required - WHY WHY WHY???


coderb

Recommended Posts

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> 

Link to comment
https://forums.phpfreaks.com/topic/105715-object-required-why-why-why/
Share on other sites

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>

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>

  • 2 weeks later...

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.