views:

29

answers:

2

Hello,

I have multiple buttons in a form (all are type 'submit'). I would like to determine which one of them is clicked. I am currently using:

$(".MyForm").click(function() {
    ...
}

to get values from my form. Any help is greatly appreciated.

A: 

Inside your handler, 'this' is the DOM element that was clicked, and $(this) is the jQuery object for that DOM element. So...

$(".MyForm").click(function() {
  var buttonId = $(this).attr('id');
  //...
}
sje397
Works great - thanks :)
Aamir Mansoor
A: 

You define an id for each button then you can use $("#buttonid").click to define different action for each button you like or test the id with .attr('id')

iKid
The number of buttons for each form is not fixed - would this approach still work? The # of buttons are populated from database and could be different each time the page loads
Aamir Mansoor
@Aamir Mansoor: Yeah, I also thought about it. if that's the case then I guess the javascript also need to generate from the server side. If you have other idea, it'd be great to hear.
iKid