#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    66
    Rep Power
    3

    Remove undefined string


    Hello
    Code:
    <!DOCTYPE html>
    <html>
    <body>
    
    <canvas id="myCanvas">Your browser does not support the HTML5 canvas tag.</canvas>
    
    <p id="demo">hello</p>
    
    
    <script>
    function bbb()
    {
    var c=document.getElementById('myCanvas');
    var ctx=c.getContext('2d');
    ctx.fillStyle='#FF0000';
    ctx.fillRect(0,0,80,100);
    }
    var d = new Date();
    var a =d.getHours();
    
    if(a==17)
    {
    document.getElementById("demo").innerHTML=bbb(); 
    }
    
    </script>
    
    </body>
    </html>
    when I run this code in the browser it works but underneath the red rectangle there is a string that says "undefined" how do I get rid of this? thanks
  2. #2
  3. CSS & JS/DOM Adept
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2004
    Location
    USA (verifiably)
    Posts
    20,127
    Rep Power
    4304
    bbb doesn't return a value, so you're assigning "undefined" (which is automatically converted to a string) to the innerHTML property "#demo".
    Spreading knowledge, one newbie at a time.

    Check out my blog. | Learn CSS. | PHP includes | X/HTML Validator | CSS validator | Common CSS Mistakes | Common JS Mistakes

    Remember people spend most of their time on other people's sites (so don't violate web design conventions).

IMN logo majestic logo threadwatch logo seochat tools logo