How to bind jquery event before event in tag is executed
4 answers
My guess is the built-in onclick handler cannot be removed as it was inferred by ASP.NET. To solve this problem, you need to override the ASP.NET doPostBack method to inject your function before the postback is done. Try to include the following function on your page and then use it to add an event handler:
Update: Just re-read and realize that this answer misses the mark a bit - it basically lets you fire events before postbacks, but doesn't have to bind to an onclick handler.
var addToPostBack = function(func) {
var old__doPostBack = __doPostBack;
if (typeof __doPostBack != 'function') {
__doPostBack = func;
} else {
__doPostBack = function() {
func();
old__doPostBack.apply(this, arguments);
}
}
};
...
<input type="button" id="btn" onclick="..." />
...
addToPostBack(function() { alert("You're posting back!"); });
0
source to share
If you are using jQuery, there is no need to embed onclick in HTML:
$('input').click(function() {
//this first
something();
// this last
playSound();
});
Edit:
Just remove the onclick attribute with jQuery:
$('input').removeAttr("onclick");
you can put it inside the document ready, or just attach it to your call
$('input').removeAttr("onclick").click(function() {...});
0
source to share