Jump to content

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...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.