Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative to document.getElementById().setAttribute() function

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.

like image 534
Ali Avatar asked Jun 19 '26 17:06

Ali


2 Answers

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?
  });    
});
like image 184
Jose Basilio Avatar answered Jun 21 '26 08:06

Jose Basilio


If you can use jQuery, then:

$("#btn").click(function() { save(id); })
like image 44
Ayman Hourieh Avatar answered Jun 21 '26 06:06

Ayman Hourieh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!