Diogo Garcia Diogo Garcia - 3 months ago 15
Javascript Question

Get element index using class and subclass

I've been trying to get the index of a TR element which has two class the first one identify it like a group and the second one as a sub-group, then I try to get the index of a specific group and subgroup it returns me -1... Does anyone know what am I doing wrong?

Here is an example:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<table>
<tr class="trusure1 trrstatusC"></tr>
<tr class="trusure2 trrstatusC"></tr>
<tr class="trusure2 trrstatusC"></tr>
<tr class="trusure2 trrstatusR"></tr>
<tr class="trusure2 trrstatusR"></tr>
<tr class="trusure3 trrstatusC"></tr>
<tr class="trusure3 trrstatusC"></tr>
</table>
<div id="console"></div>
<script>
$('#console').append('<br> The tr with the class trusure2 and the lastest with class trrstatusR is on the position '+$('tr.trusure2.trrstatusR:last-child').index());
<script>


https://jsfiddle.net/nod9xbsh/

Thank you!!

Answer

You should use $('tr.trusure2.trrstatusR:last').index() instead