-1

I have two button that run the same script but create different parameter.

<input type='button' id='btn_a' value='button_a' /><br />
<input type='button' id='btn_b' value='button_b' /><br />

How to set the button to create parameter for each which is pressed? something like this perhaps.

$('#btn_a, #btn_b').on('click', function(){
     if( $('#btn_a').data('clicked', true)) {
        //Create parameter for button A
    }

    if( $('#btn_b').data('clicked', true)) {
        //Create parameter for button B
    }

    //script for sent the parameters
});
Shota
  • 515
  • 3
  • 18
  • 1
    Possible duplicate of [Getting the ID of the element that fired an event](https://stackoverflow.com/questions/48239/getting-the-id-of-the-element-that-fired-an-event) – caramba Feb 21 '18 at 06:48

2 Answers2

1

You can compare the id of the clicked button.

this.id === "btn_a"

or

event.currentTarget.id == "btn_a"

i.e. use a switch-case to do button specific operation

$(function(){
    $('#btn_a, #btn_b').on('click', function(){
        switch( this.id )
        {  
           case "btn_a":
            //Create parameter for button A
            console.log(this.id)
           break;
           case "btn_b":
            //Create parameter for button B
            console.log(this.id)
           break;
           default:
        }
    });
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type='button' id='btn_a' value='button_a' /><br />
<input type='button' id='btn_b' value='button_b' /><br />
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
0
$('#btn_a, #btn_b').on('click', function(e){
   alert(e.target.id);
});

This should work and give you the element (target) ID.

Tarek N. Elsamni
  • 1,718
  • 1
  • 12
  • 15