Thread: Noob Question

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

    Join Date
    Sep 2012
    Posts
    66
    Rep Power
    3

    Noob Question


    Hello I started learning javascript about a week ago so this is without a doubt a super simple question.
    Code:
    <!DOCTYPE html>
    <html>
    <body>
    
    </body>
    <script>
    var fiat = {1:"900kg";2:"4 seats";3:"40 horse power";4:"custom colors"};
    function weight()
    {
    document.getElementById("w").innerHTML= "The weight is" + fiat.1;
    }
    function seats()
    {
    document.getElementById("seats").innerHTML= fiat.2 + "are in the car";
    }
    </script>
    <p id="w"> What's the weight? </p>
    <button type="button" onclick="weight()">Weight</button>
    <p id="seats">How many seats?</p>
    <button type="button" onclick = "seats()">How many?</button>
    </html>

    when I click the buttons in my browser (I am using Safari) nothing happens. I figure that this is probably a syntax problem but I could not find the proper syntax anywhere. Thanks a bunch
  2. #2
  3. Old Fart
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Location
    Chicago
    Posts
    106
    Rep Power
    4
    Put your paragraphs and buttons IN the body of the page.
  4. #3
  5. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,173
    Rep Power
    184
    The first key in each set of your JSON variables; was not double quoted and you did not call the JSON key accurately..., it should be like this:
    Code:
    var fiat = {"1":"900kg","2":"4 seats","3":"40 horse power","4":"custom colors"};
    function weight()
    {
    document.getElementById("w").innerHTML= "The weight is " + fiat[1];
    }
    function seats()
    {
    document.getElementById("seats").innerHTML= fiat[2] + " are in the car";
    }
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    66
    Rep Power
    3
    thanks

IMN logo majestic logo threadwatch logo seochat tools logo