how to change onclick event with jquery?
Remove the old event handler
$('#id').unbind('click');
And attach the new one
$('#id').click(function(){
// your code here
});
You can easily change the onclick
event of an element with jQuery without running the new function with:
$("#id").attr("onclick","new_function_name()");
By writing this line you actually change the onclick
attribute of #id
.
You can also use:
document.getElementById("id").attribute("onclick","new_function_name()");
Updating this post (2015) : unbind/bind should not be used anymore with jQuery 1.7+. Use instead the function off(). Example :
$('#id').off('click');
$('#id').click(function(){
myNewFunction();
//Other code etc.
});
Be sure that you call a non-parameter function in .click, otherwise it will be ignored.