views:

1133

answers:

3

Is there any alternative to doing the following line:

 document.getElementById("btn").setAttribute("onclick", "save(" + id + ");");

This line basically changes the onclick() event of a button to something like: save(34); , save(35); etc. However it fails in IE 7 and 6 but works in IE 8 and Firefox.

I can use jquery for this as well.

+8  A: 

If you can use jQuery, then:

$("#btn").click(function() { save(id); })
Ayman Hourieh
A: 

Also this one :

$("#btn").bind('click', function() { save(id); });
Canavar
+5  A: 

Plain old javascript:

var myButton = document.getElementById("btn");
myButton.onclick = function()
{
  save(id); //where does id come from?
}

jQuery:

$(function(){
  $("#btn").click(function(){
    save(id); //where does id come from?
  });    
});
Jose Basilio
Great reply, Jose!
Sergey Ilinsky