Prev: FAQ Topic - Internationalisation and Localisation in javascript (2010-07-19)
Next: how to escape and pass html as variable
From: Garrett Smith on 20 Jul 2010 13:54 On 2010-07-20 09:37 AM, Kevin wrote: > function saveXML2(StringQuery) > { > var myButton = document.getElementsByName("saveButton") > [0].childNodes[0]; > > myButton.onclick = print(); > // myButton.onclick = new Function( "print()"); > // myButton.onclick = print; > > } > > function print() > { > alert("print"); > } Browsers have a window.print method and so if this is global code, you should use a different name. Assignment to event handler properties requires a function. Some browsers accept a string but that is not interoperable. The code: myButton.onclick = print(); invokes the `print` method and assigns the result to myButton.onclick. The `print` method returns undefined and so the assignment is equivalent to: myButton.onclick = undefined; -- not what you wanted. Try: <button id="saveButton">...</button> <script type="text/javascript"> var saveButton = document.getElementById("saveButton"); saveButton.onclick = saveButtonClickHandler; function saveButtonClickHandler(ev) { alert(this.tagName); } </script> Possibly related: <http://jibbering.com/faq/#accessElementBeforeDefined> -- Garrett |