Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By Sasseke

Thread: A bug in JS code

  1. #1
    Member Sasseke's Avatar
    Join Date
    Oct 2014
    Location
    Groningen, Netherlands
    Posts
    45
    Member #
    40386
    Liked
    2 times

    A bug in JS code

    Hello
    my website is Homepage van Diana van Landeghem
    In the index file there's a <h1>..</h1> which I gave the id "groet"
    I want to change the text of this "groet" to the following:

    var groet = document.getElementById("groet")

    var todaydate = new Date();
    var timeis = todaydate.getTime();
    todaydate.setTime(timeis);
    var houris = todaydate.getHours();
    var display;

    if (houris >= 18 && houris < 24)
    {
    display = "navond";
    }
    else if (houris >= 24 && houris < 6)
    {
    display = "nacht";
    }
    else if (houris > 12 && houris <= 18)
    {
    display = "middag";
    }
    else
    {
    display = "morgen";
    }

    var welcome = "Goede" + display + ". Welkom op de homepage van Diana van Landeghem.";
    groet.innerHTML = welcome;

    In Firefox it says: TypeError: groet is null on line 27.
    I don't know why it says this and neither, why the text of <h1>..</h1> isn't changed.

    I learned some JavaScript but in practice I'm still a beginner.
    Can someone help me with this (bug)?
    Sincerely
    Sasseke

  2.  

  3. #2
    Senior Member sasha_bolcina's Avatar
    Join Date
    Sep 2014
    Location
    Serbia
    Posts
    274
    Member #
    40099
    Liked
    47 times
    I cannot find <h1 id='groet'> on your index page.

    Alert is displayed well, just missing a space after "Goede" ( var welcome = ("Goede " + display... )

    Edit:
    Wooow ... Now I see the problem...
    Your script is executing before the DOM is loaded.... Put the script on the bottom of the page, or make a function and call it after the DOM is loaded (just before the end body tag </body> )

  4. #3
    Member Sasseke's Avatar
    Join Date
    Oct 2014
    Location
    Groningen, Netherlands
    Posts
    45
    Member #
    40386
    Liked
    2 times
    Hi Sasha
    indeed, in the index.html of my uploaded website there still wasn't an id for this <h1>..</h1> but now there is.
    I put the code at the end of my index.html and it works!

    Thank you very much
    Sass
    sasha_bolcina likes this.

  5. #4
    Senior Member sasha_bolcina's Avatar
    Join Date
    Sep 2014
    Location
    Serbia
    Posts
    274
    Member #
    40099
    Liked
    47 times
    Welcome


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 08:48 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com