1

As easy as it sounds but I am stuck in retrieving the row number of the current clicked/selected row.

I have tried the following so far. I am using bootstrap table just in case you want to know.

$('#myTable').on('click-row.bs.table', function (e, row, $element) {

     //var row_num = parseInt($(this).parent().index()) + 1;

     //var row_num = $(this).closest('tr').index();

     //var row_num = $(this).closest('td').parent()[0].sectionRowIndex;

}

None seem to be working for me.

4
  • 3
    possible duplicate of jQuery: Which row number is clicked in table Commented May 18, 2015 at 19:54
  • If you add your html and describe what you want to achieve (for example if a row is clicked it should get a different background) we can help you better. Could you explain why the answers did not work for you?. Commented May 18, 2015 at 19:56
  • @IvanSivak, tried both the answers there...didnt work for me. Commented May 18, 2015 at 19:57
  • @threeFourOneSixOneThree....the answers are always returning 1 if i do a +1 or in other words 0 Commented May 18, 2015 at 20:10

2 Answers 2

4

Th problem you have is simply about the way that click-row.bs.table works, as far as click-row.bs.table is a table event, this keyword points to the table itself not to the row, so you just need to use the $element instead:

$('#myTable').on('click-row.bs.table', function (e, row, $element) {
  var row_num = $element.index() + 1;
});
Sign up to request clarification or add additional context in comments.

1 Comment

you missed the opening braces there ( - for element
-2

Its better to use the data attribute with the index

$element.data('index')

The method .index() will change if there are rows added for example by using expandRow

// Open or close on click
$table.on('click-row.bs.table', function (e, row, $element, index) {
  if(!row.open){
    $table.bootstrapTable('expandRow', $element.data('index'));
  }
  else{
    $table.bootstrapTable('collapseRow', $element.data('index'));
  }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.