Forum Moderators: open

Message Too Old, No Replies

getElementById returns empty

         

wadesmart

12:38 am on Mar 9, 2008 (gmt 0)

10+ Year Member



I purchased Simply Javascript from Sitepoint and Im working on some
of the tutorials and none of them are working. I do not understand
what is wrong. Each lesson builds on the last and I have had problems right from the beginning.

The html page:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
<head>
<title>Get Tag By ID</title>
<meta http-equiv="Content-Type" content="text/html;
charset=utf-8" />
<link rel="stylesheet" type="text/css" href="pg84.css" />
<script type="text/javascript" src="pg84.js" ></script>
</head>

<body>
<a id="koko" href="http://www.example.com/">Get KOKO!</a>
</body>
</html>

And here is the script:
var koko = document.getElementById("koko");
koko.setAttribute("href", "/GetKoko/");
koko.setAttribute("title", "Koko sets the title");

And the error is:
koko has no properties
line1: koko.setAttribute("href","/GetKoko/");

I have also tried:
koko.href = '/GetKoko/'
koko.title = 'Koko sets the title';

Im using Firefox 2.0.0.12 using Firebug 1.05 extension.

Wade

MarkFilipak

1:00 am on Mar 9, 2008 (gmt 0)

10+ Year Member



Return the book and get your money back.

The script is in the head-element (where it should be) but when it runs, the body-element doesn't exist yet, so your a-element doesn't exist yet. Hence, document.getElementById("...anything...") will always fail. What to do?

<head>...
<script type="text/javascript"><!--
function getKoko() {
var koko = document.getElementById("koko");
koko.setAttribute("href", "/GetKoko/");
koko.setAttribute("title", "Koko sets the title");
}
//--></script>
...</head>
<body>...
<a id="koko" href="http://www.example.com/" onclick="getKoko()">Get KOKO!</a>
...</body>

[edited by: MarkFilipak at 1:10 am (utc) on Mar. 9, 2008]

rocknbil

4:04 pm on Mar 9, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, if that script is wrapped by window.onload() it will work, unobtrusively, without an onClick event handler in the code . . . .

window.onload = function () {
var koko = document.getElementById("koko");
koko.setAttribute("href", "/GetKoko/");
koko.setAttribute("title", "Koko sets the title");
};

Not sure why yours isn't working, a test is pasted below that does work.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<!-- doctype all on one line!-->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Untitled</title>
<script type="text/javascript">
window.onload = function () {
var koko = document.getElementById("koko");
koko.setAttribute("href", "/GetKoko/");
koko.setAttribute("title", "Koko sets the title");
};
</script>
</head>
<body>
<a id="koko" href="http://www.example.com/">Get KOKO!</a>
</body>
</html>

httpwebwitch

4:35 pm on Mar 9, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



try the onDomReady event [docs.mootools.net], and learn why it's better than onLoad [demos.mootools.net]

vol7ron

6:34 pm on Mar 9, 2008 (gmt 0)

10+ Year Member



<link rel="stylesheet" type="text/css" href="pg84.css" />
<script type="text/javascript" src="pg84.js" ></script>

Are you sure the path to the script and style files are correct. Make sure they're in the same directory with your HTML file. Otherwise, you'll need to put "./path/pg84.js"

wadesmart

12:29 am on Mar 10, 2008 (gmt 0)

10+ Year Member



The init() function did the trick. As Im very very new (obviously) to js I didnt even think about the js loading first and therefore couldnt load the html because its not there yet.