1

I am trying to get the row index of selected row in html table using below code but it doesn't work.

    $(function() {

        $("#myTable tr").click(function() {
            alert(this.rowIndex);
        });
    });

What must be the problem with the above code?

5
  • yes sir it doesn't show up Commented Oct 7, 2015 at 2:52
  • @J-J It does show up. Make sure to add reference to jQuery Commented Oct 7, 2015 at 2:53
  • @NorlihazmeyGhazali..I already added the reference but still doesn't work Commented Oct 7, 2015 at 2:55
  • have a look at this jsfiddle.net/fwftob08 Commented Oct 7, 2015 at 2:56
  • I think your code looks fine. Did you mismatch your element id myTable? Commented Oct 7, 2015 at 3:03

3 Answers 3

3

Your code look fine, unless you did't add jQuery references like other said AND/OR maybe your table was created dynamically, try using .on for event delegation like so :

$(function() {
    // use event delegation
    $(document).on('click','#myTable tr', function() {
        alert(this.rowIndex);
        // or
        alert($(this).index()); // jQuery way
    });
});
Sign up to request clarification or add additional context in comments.

1 Comment

Be careful with dynamic content. Glad to hear that and you're welcome.
0

$('#table tr').each(function(indx, val) {
  $(this).click(function() {
    var index = $(this).index();
    var value = $(this).text();
    alert('This row index is ' + index + ' This row value is ' + value)
    console.log(indx)
  })
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table id='table'>
  <tr>
    <td>qwe</td>
  </tr>
  <tr>
    <td>123</td>
  </tr>
  <tr>
    <td>123qwe</td>
  </tr>
</table>

You can use .index() to get the index of tr.

Description: Search for a given element from among the matched elements.

Documentaion

Also you can use the parameter using .each function

Description: Iterate over a jQuery object, executing a function for each matched element.

Documentaion

3 Comments

How will it trigger on button click sir?
just add it inside the click event like $('#someid').click(function(){put it here})
I mean when row is clicked sir
0

Same problem I tried to solve few days back. You can use this code as a template for similar problems. I will comment on the way.

    window.onload = function() {

    /* variable stores html data as array for desired tag, this case it's 'tr' -*/

          var test = this.test = $('tr'); 

/* loop traverses the row elements in the array, which element is clicked */

    for (var i = 0; i < test.length; i++){
          index = Array.prototype.indexOf.call(test,test[i]);

/* it's best to store the index inside the original row element which eases the access */

          test[i].setAttribute('index',index); 

    /* on clicking the element it calls for a function which alerts the index */

          test[i].onclick = alertTheClick ;

    /* this is in case of debug */
          console.log(index);

       }
    };

    /* function definition */
    function alertTheClick(index){       /* index value from loop */
       alert(this.getAttribute('index'));  /* index attribute from 'tr' element */
    }

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.