adib16
adib16

Reputation: 1697

find last html element with jquery for special attribute

I have a table. Always multiple row in the table contain a attribute like selectrow="selectrow". For example 3rd and 4th row have selectrow="selectrow" attribute. Now i want to find out the index of last row that have selectrow="selectrow" attribute. I do not want to use each. I search for solution like this :

$("table > tbody > tr[selectrow='selectrow']:last-child").index();

This is html:

<table>
 <tbody>
  <tr >
  </tr>
  <tr>
  </tr>
  <tr selectrow="selectrow">
  </tr>
  <tr selectrow="selectrow">
  </tr>
  <tr>
  </tr>
  <tr>
  </tr>
 </tbody>
</table>

In this example i want to get 4.

Upvotes: 0

Views: 53

Answers (2)

Bhushan Kawadkar
Bhushan Kawadkar

Reputation: 28513

Try this : use :last which will give you last of the matched selection. :last-child will select the last child of table and try to match the other selection criteria (selectrow='selectrow' in your case) and when match not found then it will retrun -1.

$("table > tbody > tr[selectrow='selectrow']:last").index();

JSfiddle Demo

More Information on
:last
:last-child

Upvotes: 4

Mox Shah
Mox Shah

Reputation: 3015

Try this

$("table > tbody > tr[selectrow='selectrow']").last();

it will return the object of last tr.

Upvotes: 1

Related Questions