views:

214

answers:

2

I have 3 buttons <input type="submit" name="submitButton" value="something">. Then i check the event with $(".form_name").submit(function(e) { and i would like to check which of the 3 buttons have been clicked and to alert the user of the action and return false if the user wishes to not go through with the action.

How do i check which of these buttons have been clicked?

+1  A: 

The Event interface gives information about the event. Look at this answer to know where you can find this object.

The target or scrElement of the object is the attribute you're looking for. See Quirksmode.org.

EDIT: you asked for a jQuery specific answer. You can just use event.target (in your case e.target), jQuery fixes this when necessary.

Marcel Korpel
+2  A: 

event.target might help. Another workaround is to have the three buttons fire events, and make their event handler do the submission. In that sense there is no guesswork.

Personally I would first make sure that the three buttons have identifiers, plausibly tucked within with $(element).data lines, or custom HTML attributes (oh, don’t use special classes!), and have the event handler check $(event.target).data(key) or $(event.target).attr(key) to find out more information related to the caller. I would then map the form’s submit event also to this handler, so everything is processed from one place.

Evadne Wu
+1 I would also bind three separate events to those three buttons.
Marcel Korpel
turns out you can return true/false on a click event to stop a form from being submitted. +1 + accepted
acidzombie24