$('input').click(function() { //thi...">

How to bind jquery event before event in tag is executed

<input type="button" onclick="executes second" />

$('input').click(function() { //this first }}

      

ps onclick = "does the second" - cannot be removed (its __doPostBack)

+2


source to share


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


try ...



$(document).ready(function(){
    $("input").removeAttr("onclick");
    $("input").click(function(){
        // do first
        my_first_function();                
        // do second
        my_second_function();
        return false;               
    });
});

      

+4


source


Could you just do something like:

< input type="button" onclick="do_something();" />

function do_something() {
    // this first
    // then second?
 }}

      

+2


source


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







All Articles