Jakob Alexander Eichler
Jakob Alexander Eichler

Reputation: 3056

How to figure out which button was clicked

Hello I have a form which has two buttons. In the form validation (onsubmit callback), I would like to know which of the two submit buttons which both share the same name were clicked.

How can I do that using JQuery?

Upvotes: 2

Views: 171

Answers (4)

moey
moey

Reputation: 10887

You can try:

$("form").submit(function() {
  if ($(this).attr("id") == "some_id") {
    // Do something ...
  }
}

Upvotes: 1

dku.rajkumar
dku.rajkumar

Reputation: 18568

make use of this as

var button_id = $(this).attr('id');

Upvotes: 0

Wasim Karani
Wasim Karani

Reputation: 8886

Jsfiddle Link

HTML

<input type="button" name="sameName" class="buttonClass" id="Button1" value="Button1">
<input type="button" name="sameName" class="buttonClass" id="Button2" value="Button2">

Jquery

$(".buttonClass").click(function(){
    alert($(this).attr("id")+" clicked");
})

Upvotes: 2

ridecar2
ridecar2

Reputation: 1957

Use event.target (assuming the callback's argument is called event.): http://api.jquery.com/event.target/

Upvotes: 0

Related Questions