Skip to content Skip to sidebar Skip to footer

How To Get The Id When A Certain Button Is Clicked Using Jquery

How can I get the id of tbody's input when #moveToAnTable is clicked using jQuery? Thanks.

Solution 1:

Use jQuery.closest to get the table of the button. After that search for the input.

jQuery.attr will return the attribute of the first element in the match:

$("#moveToAnTable").click(function(){
  var id = $(this).closest("table").find("tbody input").attr("id");
  alert(id);
});

See the fiddle:

http://jsfiddle.net/URGVp/

Solution 2:

You can also write it like this with the on method, and test out your results in a console window if that better suits you, rather than constant pop-ups.

    $('#moveToAnTable').on("click",function(){
      var mvalue = $(this).closest('table').find('tbody input').attr('id');
      console.log('my value is: ' + mvalue);
    });

Post a Comment for "How To Get The Id When A Certain Button Is Clicked Using Jquery"