views:

377

answers:

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

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

p.s. onclick="executes second" - cannot be removed (its __doPostBack)

A: 

If you use jQuery the is no need to put onclick into the HTML:

$('input').click(function() {
    //this first
    something();
    // this last
    playSound();
});

Edit:
Just Remove the onclick attribute with jQuery:

$('input').removeAttr("onclick");

you could put it inside document ready or just attach it to your call

 $('input').removeAttr("onclick").click(function() {...});
googletorp
I am not putting it in ... it is doPostBack!!!!!
dynback.com
This would stop the postback firing which may not be desirable.
Luke Bennett
@Luke The idea was to fire it with jQuery instead.
googletorp
+2  A: 

Couldn't you just do something like:

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

function do_something() {
    // this first
    // then second?
 }}
Roberto Aloi
This is not ideal, if do_sometihng() was originally defined elsewhere and is not easily copied over etc.
googletorp
A: 

My guess is the inline onclick handler cannot be removed as it has been output by ASP.NET. To solve this problem you need to override ASP.NET's doPostBack method to inject your function before the postback takes place. Try including the following function in your page somewhere and then use it to add your event handler:

Update: Just re-read and realised this answer slightly misses the mark - it basically allows you to fire events prior to postback, but not necessarily attached to the button's 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!"); });
Luke Bennett
A: 

try ...

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