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.
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.
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?
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.