Take the 2-minute tour ×

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

<table id="yearSectionAdd2Table">
      <tr id="columnHeader">
               <input id="moveToAnTable" type="button" value="&lt;&lt;">
             <input id="moveToAnTableOne" type="button" value="&lt;&lt;">
share|improve this question
@billyonecan They look unique to me, the one in the tbody has "One" on the end. –  Anthony Grist May 13 '13 at 14:07
@AnthonyGrist right you are, apparently I can't read –  billyonecan May 13 '13 at 14:07
add comment

2 Answers

up vote 1 down vote accepted

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:

  var id = $(this).closest("table").find("tbody input").attr("id");

See the fiddle:


share|improve this answer
add comment

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.

      var mvalue = $(this).closest('table').find('tbody input').attr('id');
      console.log('my value is: ' + mvalue);
share|improve this answer
add comment

Your Answer


