tags:

views:

59

answers:

2

I need a list of URLs to apply filters to and at the moment I write $('.classname a') but now I need only the first link of each list. How do I get this using jQuery?

+5  A: 

If the link is a direct child of the list, then the best solution would be:

$('ul.classname > li > a:first-child')

I'm not sure but I think the other solutions will only get you one link (i.e. the first link of the first list). EDIT: yep, tested this and :first will always return one element, not one for each parent element matched.

DisgruntledGoat
I cant believe i got 3 of the same wrong answers. Thanks. +1 and accept.
acidzombie24
A: 

The following will match the first CHILD (most likely your A) in the first LI in every UL with class "classname".

$("ul.classname > li:first-child")

http://pastebin.me/6865fdea79f3224c00a72e023f77a44a

Fabian