views:

179

answers:

4

I've got an onsubmit handler added to a form like so:

    $('#content_form').bind('submit',function(e) {

        source = $(e.target).attr('name');
        alert(source);
        return false;
    });

so e.target = the form element. I'm using several submit buttons, and need to determine which one was actually clicked (in modern browsers, that clicked button is the only one that submits, I'm doing this for IE6 compat - it submits the values of all the buttons).

My only thought it to kill any onsubmit events, and then tie click events to the buttons themselves. This would kill the form functionality entirely if javascript wasn't enabled, so I'd like to avoid this.

+1  A: 

An easy (but possibly naive) implementation would be to have the onclick handler for each button set a field indicating which one was the last one clicked. In your submit handler, you could then check the value of this field.

John Feminella
This would work, but I think I'm going to attack it a different way, after reading responses and considering. I was hoping there way an easy way to say "yep, it originated with X" but apparently not :)
Erik
Write back and tell us what you did! I'm curious to know how you solved the problem.
John Feminella
+1  A: 
$('#content_form input:submit').bind('click', function(e) {
    $('#content_form').submit();

    // you can now reference this or $(this),
    // which should contain a reference to your button
});
Matt Huggins
This doesn't work properly because a 'enter' key can trigger the onsubmit event. The more I think about it, the more I think I'm approaching the UI from the wrong direction
Erik
A: 

Have you checked out the jQuery Form Plugin? It handles submitting forms via ajax very nicely and will handle this problem (along with many others) for you.

PetersenDidIt
A: 

Something else you could do is use preventDefault(); instead of return false

Joseph Silvashy