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

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

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

heres the full code:

<body onload="aaa()">

<script type="text/javascript">

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



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


Here is a picture, of how it looks:


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.