Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find the last "li" in a "ul" using jQuery?

Tags:

jquery

I want to check the li that is the last li in ul. How can I check that using jQuery?

<ul id="ulscroller">
    <li value="1" class="selected">1</li>
    <li value="2">2</li>
    <li value="3">3</li>
    <li value="4">4</li>
    <li value="5">5</li>
    <li value="6">6</li>
    <li value="7">7</li>
    <li value="8">8</li>
    <li value="9">9</li>
    <li value="10">10</li>
    <li value="11">11</li>
    <li value="12">12</li>
    <li value="13">13</li>
    <li value="14">14</li>
    <li value="15">15</li>
    <li value="16">16</li>
    <li value="17">17</li>
    <li value="18">18</li>
    <li value="19">19</li>
    <li value="20">20</li>
</ul>
like image 444
A.T. Avatar asked Jan 25 '13 18:01

A.T.


1 Answers

Just use the :last-child selector :

$('#ulscroller li:last-child')

DEMO: http://jsfiddle.net/f5v6R/

For example, if you want to know if it has the selected class you may do

if ($('#ulscroller li:last-child').hasClass('selected')) {
   // do something
}
like image 62
Denys Séguret Avatar answered Oct 12 '22 22:10

Denys Séguret