How to get index of clicked button from the grid of buttons?

Viewed 3671

This is my HTML...

$("table tr button").on('click', function(e) {
  var i = $(this).closest('td').parent()[0].sectionRowIndex;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="tab">


  <tr>
    <td><button class="button"></button></td>
    <td><button class="button"></button></td>
    <td><button class="button"></button></td>
    <td><button class="button"></button></td>

    <td><button class="button"></button></td>

    <td><button class="button"></button></td>

  </tr>

  <tr>
    <td><button class="button"></button></td>
    <td><button class="button"></button></td>
    <td><button class="button"></button></td>
    <td><button class="button"></button></td>

    <td><button class="button"></button></td>

    <td><button class="button"></button></td>

  </tr>
</table>

I was trying to get the index of clicked button.

But I am getting an error...please somebody tell me how to do this.

3 Answers

You can use closest and index from jquery. Each row will have an index starting from 0 & the index of the button will be relative to that row.

Also you can delegate the event from table to button by replacing $("table tr button").on('click', function(e) { with $("table").on('click', 'button', function(e) {

$("table").on('click', 'button', function(e) {
  var rowIndex = $(this).closest('tr').index();
  var colIndex = $(this).closest('td').index()
  console.log(`Row index is ${rowIndex} & button index is ${colIndex}`)
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="tab">


  <tr>
    <td><button class="button">Click</button></td>
    <td><button class="button">Click</button></td>
    <td><button class="button">Click</button></td>
    <td><button class="button">Click</button></td>

    <td><button class="button">Click</button></td>

    <td><button class="button">Click</button></td>

  </tr>

  <tr>
    <td><button class="button">Click</button></td>
    <td><button class="button">Click</button></td>
    <td><button class="button">Click</button></td>
    <td><button class="button">Click</button></td>

    <td><button class="button">Click</button></td>

    <td><button class="button">Click</button></td>

  </tr>
</table>

Since you're using jQuery you could simply use the index() method like:

Index of the button in the row/table:

$("table tr button").on('click', function(e) {
  var index_in_row   = $(this).closest('td').index();
  var index_in_table = $('button').index($(this));

  console.log('Index of the button in row: ' + index_in_row);
  console.log('Index of the button in table: ' + index_in_table );
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="tab">
  <tr>
    <td><button class="button">button 0</button></td>
    <td><button class="button">button 1</button></td>
    <td><button class="button">button 2</button></td>
    <td><button class="button">button 3</button></td>
  </tr>
  <tr>
    <td><button class="button">button 4</button></td>
    <td><button class="button">button 5</button></td>
    <td><button class="button">button 6</button></td>
    <td><button class="button">button 7</button></td>
  </tr>
</table>

You can store the index value in the custome data atrributes like shown below

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="tab">


  <tr>
    <td><button data-index='0' class="button">Click</button></td>
    <td><button data-index='1' class="button">Click</button></td>
    <td><button data-index='2' class="button">Click</button></td>
    <td><button data-index='3' class="button">Click</button></td>

    <td><button data-index='4' class="button">Click</button></td>

    <td><button data-index='5' class="button">Click</button></td>

  </tr>

  <tr>
    <td><button data-index='6' class="button">Click</button></td>
    <td><button data-index='7' class="button">Click</button></td>
    <td><button data-index='8' class="button">Click</button></td>
    <td><button data-index='9' class="button">Click</button></td>

    <td><button data-index='10' class="button">Click</button></td>

    <td><button data-index='11' class="button">Click</button></td>

  </tr>
</table>

then after clicking on button you can get the index value via jquery like this

$('.button').click(function(){
    let index = $(this).data('index');

    console.log(index);

});
Related