As a cross-browser, using the DOM, you can add an onClick attribute and so that the function can take parameters: for example, obj.setAttribute("onclick","play(1);"); does not work and IE if you write obj.onclick=help; (function without parameters) then everything works and if obj.onclick=play(2); then play(2); starts itself to be executed even before pressing (obviously as in the case of var i = func (3) by assigning the returned value), but as having a link to the object dynamically cross-browser add an onClick attribute with a function with a parameter so that the function is executed only when pressed?

  • I know js very badly, but it seems to be necessary to omit the arguments of the play function. obj.onclick = play; - culebre

3 answers 3

 obj.onclick = function(){ play(2); } 

or

 var help = function() { play(2); } obj.onclick = help; 
  • Thanks the only thing that works fine! - Rules

If you use jQuery, then simply, put the object id="someID" , and then:

 $("#someID").click( function () { play(2); } ); 

Without jQuery:

 function setEvent(c, name, action) { if (c.addEventListener) { c.addEventListener( name, action, false ); } else if (c.attachEvent) { c.attachEvent("on" + name, action); } else { c["on" + name] = action; } } 

Accordingly use:

 setEvent(document.getElementById("someID"), "click", function () { play(2); } ); 
  • better use jQuery.live ("click", handler) - Specter
  • And I ask how without Jquery and with the parameters, because in Jquery, it was implemented somehow? - Rules
  • And when I have not heard about addEventListener, and in my book there is no this thanks! - Rules

Without jquery

 function $(id) { return document.getElementById(id); } document.onreadystatechange = function() { $('someID').onclick = function() { //function } } 

Very cross-browser!

  • Then: document.onreadystatechange = function () {if (document.readyState == "complete") {...}} - chernomyrdin