views:

88

answers:

3

Suppose a HTML markup like this:

<ul>
    <li>List item 1</li>
    <li>List item 2</li>
    <li>List item 3</li><!-- I want to select only this -->
</ul>

Would it be possible to select only the third list item in the unordered list element? I've been browsing the jQuery documentation but I can't find any selector that could do this.

+2  A: 
$("ul li:eq(2)") // 3rd item

http://api.jquery.com/eq-selector/

hunter
This doesn't work but the Neil's answer worked. Thanks anyways :)
Richard Knop
@Richard There are some subtle differences between :eq and :nth-child which are worth reading about in the docs.:eq would work for this but may need to be tweaked slightly.
Neil Aitken
+5  A: 

You may want to use the nth-child selector

$('ul li:nth-child(3)')
Neil Aitken
A: 

here you go

$('ul>li').each(function(i){
    if(i == 2)
    {
       //your code goes here

       return;
    } 
});

cheers

Marcin
jquery is much more powerful than that.
hunter
but this can give you more then 3rd result which sometimes can be better for performance... cheers
Marcin