Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to detect which button is clicked using jQuery

Tags:

html

jquery

how to detect which button is clicked using jQuery

<div id="dBlock">
 <div id="dCalc">
  <input id="firstNumber" type="text" maxlength="3" />
  <input id="secondNumber" type="text" maxlength="3" />
  <input id="btn1" type="button" value="Add" />
  <input id="btn2" type="button" value="Subtract" />
  <input id="btn3" type="button" value="Multiply" />
  <input id="btn4" type="button" value="Divide" />
 </div>
</div>

Note: above "dCalc" block is added dynamically...

like image 670
eagle Avatar asked Oct 27 '11 16:10

eagle


3 Answers

$("input").click(function(e){
    var idClicked = e.target.id;
});
like image 100
genesis Avatar answered Oct 12 '22 16:10

genesis


$(function() {
    $('input[type="button"]').click(function() { alert('You clicked button with ID:' + this.id); });
});
like image 42
RoccoC5 Avatar answered Oct 12 '22 17:10

RoccoC5


Since the block is added dynamically you could try:

jQuery( document).delegate( "#dCalc input[type='button']", "click",
    function(e){
    var inputId = this.id;
    console.log( inputId );
    }
);

demo http://jsfiddle.net/yDNWc/

like image 21
Esailija Avatar answered Oct 12 '22 16:10

Esailija