user1032531
user1032531

Reputation: 26281

Select first element in list starting from sibling

Starting from $('#selected'), what would be the best way to select the first list item?

<ul>
 <li>first</li>
 <li>second</li>
 <li id="selected">third</li>
</ul>

Upvotes: 0

Views: 43

Answers (3)

Sachin
Sachin

Reputation: 582

try this one

$('#selected').parent().children('li')[0];

Upvotes: -1

BryanC
BryanC

Reputation: 120

$('#selected').parentNode.children[0]

or

$('#selected').parentNode.firstChild

are both options, despite not being all jQuery.

Upvotes: 0

Felix
Felix

Reputation: 38112

You can use .siblings() along with .first() or :first selector:

$('#selected').siblings('li:first') // or $('#selected').siblings('li').first()

Fiddle Demo

Upvotes: 4

Related Questions