How to retrieve div element.

M

Mike Duffy

1) I have successfully done this sort of thing before. I have re-checked
my capitalization, and looked at my other code that works. Then I removed
everything except what is needed to reproduce the error. I have even r-
typed the line with the div element to preclude unprintable characters,
and checked that the whole file is okay with the w3 validator service. (
http://validator.w3.org/ )

When the page is loaded, and function testFunc() is evaluated, it tells
me that the div element is null. I would appreciate it if someone can
tell me what I have neglected.

To save anyone from typing, I have placed a copy of the html source at:

http://pages.videotron.com/duffym/test_01.htm



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">

<html lang="en">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<title>Test</title>

<script type="text/javascript">

function testFunc()
{
alert('The element itself: '+document.getElementById("test"));
alert('The text in the div tag: '+document.getElementById
("test").innerHTML);
}

</script>

</head>

<body>

<p>Dummy text that is outside the div element.</p>

<script type="text/javascript">testFunc();</script>

<div id="test"><p>Dummy text that is in the div.</p></div>

</body>

</html>
 
S

Stevo

Mike said:
1) I have successfully done this sort of thing before. I have re-checked
my capitalization, and looked at my other code that works. Then I removed
everything except what is needed to reproduce the error. I have even r-
typed the line with the div element to preclude unprintable characters,
and checked that the whole file is okay with the w3 validator service. (
http://validator.w3.org/ )

When the page is loaded, and function testFunc() is evaluated, it tells
me that the div element is null. I would appreciate it if someone can
tell me what I have neglected.

To save anyone from typing, I have placed a copy of the html source at:

http://pages.videotron.com/duffym/test_01.htm



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">

<html lang="en">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<title>Test</title>

<script type="text/javascript">

function testFunc()
{
alert('The element itself: '+document.getElementById("test"));
alert('The text in the div tag: '+document.getElementById
("test").innerHTML);
}

</script>

</head>

<body>

<p>Dummy text that is outside the div element.</p>

<script type="text/javascript">testFunc();</script>

<div id="test"><p>Dummy text that is in the div.</p></div>

</body>

</html>

The "test" div doesn't exist at the point your code runs. Try moving the
div declaration before the code.
 
M

Mike Duffy

The "test" div doesn't exist at the point your code runs. Try moving
the div declaration before the code.

Thanks! It worked. I guess I did not really have a good idea about "scope"
as it applies in the time dimension.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,130
Latest member
MitchellTe
Top