Hi
I’m new to javascript and I’m currently working through the Sitepoint Simply Javascript
book. I’m a little bit stuck on chapter 3, Document Access.
I’ve tried out the example on accessing the node you want, like:
getElementById, etc.
I’ve copied over the example “target” out of the book to see how it works yesterday
and it worked great. Now today I tried the same example by using an “if” statement.
Now when I run the program it doesn’t seem to find the “id”. It displays the alert box
“Not found”.
Please help!
Here’s my code:
var client = document.getElementById(“name”);
if (client)
{
alert(client.nodeName);
}
else
{
alert(“Not found”);
}
I do have id called “name” inside my html h1 tag. Its an external js file, and I know the
link works.
Thanks
Lowdow
The section near the end of Chapter 2 called “Unobtrusive Scripting in the Real World” holds the key to your solution. It’s on pages 58 to 59 of the book.
Have a re-read of it and see if you can work out if the html body is available when your code is being run.
It seems to be the most common issue that comes up about that book, so it’s likely to receive a better treatment if a version 2 of the book is every put together.