Reputation:
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?
Upvotes: 0
Views: 1121
Reputation: 3200
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.
Upvotes: 2
Reputation: 21763
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.
Upvotes: 1