简体   繁体   中英

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.

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

Try this

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

it will return the object of last tr .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM