Help me out with this javascript code

Discussion in 'JavaScript' started by saadi123, Apr 21, 2010.

  1. #1
    I, just for a practice, copied the following chunk of javascript code from a book which I just bought few weeks back. Which is:



    <html>
    <head>
    <title>show properties</title>
    <script>

    function showProperties(obj,objName)
    {
    var output= "";
    for (var i in obj)
    {
    output += objName + "." + i + " = " + obj + "\n";
    alert(output);
    }
    }

    function aHouse (beds,baths,age,sqft,addr)
    {
    this.beds = beds;
    this.baths = baths;
    this.age = age;
    this.sqft = sqft;
    this.addr = addr;
    }

    myHome = new aHouse(4,2,4,3000,"1234 digit st.");
    myHut = new aHouse(1,1,12,700,"3141 pie av.");
    myLodge = new aHOuse(2,1,12,1700,"2828 Euler Blvd.");
    myEstate = new aHouse(12,9,12,20000,"1 Ego st.");

    function showMyHut()
    {
    showProperties(myHut,"myHut");
    }

    function showMyHome()
    {
    showProperties(myHome,"myHome");
    }

    </script>

    </head>
    <body>
    <form name="ShowItAll">
    <hr>
    <b>Show Properties</b>
    <hr>
    <p><input type="button" value="Show myHome" onClick="showMyHome()">
    <p><input type="button" value="Show MyHut" onClick="showMyHut()">
    <p><input type="button" value="Show MyLodge" onClick="showProperties(myLodge,"myLodge")>
    </form>
    </body>

    </html>


    The problem is that the result on clicking the buttons is not appearing at all as said in the book. Infact the buttons are not working at all. Please help me figure out the error.
     
    saadi123, Apr 21, 2010 IP
  2. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #2
    few syntax errors there. it works but it's not great code or anything. http://www.jsfiddle.net/GBufj/

    i have moved the functions to window.functionName due to the scoping of jsfiddle - you should not have to do that.
     
    dimitar christoff, Apr 21, 2010 IP
  3. saadi123

    saadi123 Well-Known Member

    Messages:
    196
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #3
    My problem is that the buttons in it are not working at all. I mean that they are appearing, but on clicking them, nothing is happening. What exactly wrong have I done?
     
    saadi123, Apr 21, 2010 IP
  4. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #4
    click on the link and read the source code carefully, the buttons do work now. you had typos like:

    <input type="button" value="Show MyLodge" onClick="showProperties(myLodge,"myLodge")> -> needs to be <input type="button" value="Show MyLodge" onClick="showProperties(myLodge,'myLodge')">

    myLodge = new aHOuse(2,1,12,1700,"2828 Euler Blvd."); -> myLodge = new aHouse(2,1,12,1700,"2828 Euler Blvd.");

    if you are doing js learning/dev, get firebug plugin for firefox and use the console to see the exact errors it's producing for clues.
     
    dimitar christoff, Apr 21, 2010 IP
  5. saadi123

    saadi123 Well-Known Member

    Messages:
    196
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #5
    Thanks for pointing out.
     
    saadi123, Apr 21, 2010 IP
  6. saadi123

    saadi123 Well-Known Member

    Messages:
    196
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #6
    Thanks for pointing out. yeah I just installed fire bug.
     
    saadi123, Apr 21, 2010 IP