Get row id using jQuery by clicking the button in that row

In my HTML table, each row has a separate identifier. Each row has a column containing a button. I can record the event of pressing these buttons. Is there a way to get the corresponding row id in the click event of this button

An example table is shown below.

<table>
 <tr id="1">
     <td>   <input type="image" id="Bttn_play" src="../../Content/images/play_button.png" name="image" /> </td>
     <td> test dat1a</td>
 </tr>
 <tr id="2">
     <td>   <input type="image" id="Bttn_play" src="../../Content/images/play_button.png" name="image" /> </td>
     <td> test data2</td>
 </tr>
</table>

I can capture a button click event using the following jQuery

$("#Bttn_play").live('click', function (event) {
  alert (row id);
  // how i get row id  corresponding to this button 
}
+3
source share
3 answers

Use closest():

$("#Bttn_play").live('click',function(){
    alert($(this).closest('tr').attr('id'));
});

It is worth noting that the duplicate is idinvalid, must be unique in the document, you must convert id='Bttn_play'to class='Bttn_play'(and change the jQuery selector to:) $('.Bttn_play').


OP ( ):

jquery? . , id

, jQuery, class ( ) . input type="image":

$('input[type="image"]')

:

$('input:image')

:

+13

jQuery.parent().

jQuery, DOM,.parent() DOM jQuery .

$("#Bttn_play").live('click', function (event) {

    var rowID = $(this).parent().parent().attr('id');
});

Here is a working demo: http://jsfiddle.net/H9rpp/

Hope this helps.

+2
source
$("#Bttn_play").live('click', function (event) {
  alert ($(this).parent().parent().attr("id"));

}
+1
source

All Articles