Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Aug 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Can't find why this line gives undefined error.

    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/try...=tryhtml_basic

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

    heres the full code:

    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:


  • #2
    Regular Coder
    Join Date
    Jan 2004
    Location
    USA
    Posts
    364
    Thanks
    12
    Thanked 6 Times in 6 Posts
    It's because spanified isn't initialized. Set it to "" and undefined should go away.
    Running Windows 7 x64

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •