Reputation: 26312
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>
Upvotes: 11
Views: 44784
Reputation: 382102
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
}
Upvotes: 29
Reputation: 2150
Just use the following
if($('#ulscroller li:last-child')[0] == liTocheck)
{
alert("This is the last li");
}
Or
if($('#ulscroller li').last()[0] == liTocheck)
{
alert("This is the last li");
}
Here liTocheck
is the li that needs to be compared
Upvotes: 0
Reputation: 2150
try this
$(function(){
alert($('#ulscroller li:last-child').val());
})
Upvotes: 1
Reputation: 5998
$('ul#ulscroller').children('li').last();
You could also do it like so:
$('ul#ulscroller').children('li:last-child');
http://api.jquery.com/last-selector/
Here's an example to illustrate it: http://jsfiddle.net/TheNix/W92vF/
Upvotes: 2