...

View Full Version : Can't find why this line gives undefined error.



Atomix
08-20-2010, 09:42 AM
Hello, can you tell me why this code gives me undefined error?

You can see the code in action here:
open this link
http://www.w3schools.com/cn/html/tryit.asp?filename=tryhtml_basic

and paste this code on the left side of the webpage, then press 'edit and click me'

heres the full code:


<html><head><title>eink</title></head>
<body onload="aaa()">

<script type="text/javascript">

function aaa() {
var word2 = document.getElementById('word'),
letters = word2.innerHTML.split(''),
spanified;
for (var k = 0, j = letters.length; k < j; k++) {
spanified += '<span class="style101">' + letters[k] + '</span>';
}
word2.innerHTML = spanified;

}


</script>

<style type="text/css">
.style101
{
background-color: #FF0000;
}
</style>
<h1 id="word">HOUSE</h1>

</body>
</html>





Here is a picture, of how it looks:

http://i33.tinypic.com/nvnk9j.jpg

qwertyuiop
08-20-2010, 10:04 AM
It's because spanified isn't initialized. Set it to "" and undefined should go away.

Philip M
08-20-2010, 10:07 AM
var word2 = document.getElementById('word'),
letters = word2.innerHTML.split(''),
spanified = "";

Oh! qwertyuiop beat me to it! :)


All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum