tags:

views:

70

answers:

7

How do I select only first-level li's? If I do 'ul li', it also selects the children. Is there a way to select only the top level and not the children using CSS? If not, I am ok with using jQuery, but how would I select it in that case too?

<ul>
    <li class="administration first">
        <a href="/administration.aspx"><span>Administration</span></a>
        <ul>
            <li class="users first selected"><a href="/administration/users.aspx"><span>Users</span></a></li>
            <li class="forms last"><a href="/administration/forms.aspx"><span>Forms</span></a></li>
        </ul>
        <div style="clear: both;"></div>
    </li>
    <li class="analytics"><a href="/analytics.aspx"><span>Analytics</span></a></li>
    <li class="options"><a href="/options.aspx"><span>Options</span></a></li>
    <li class="system last"><a href="/system.aspx"><span>System</span></a></li>
</ul>
A: 

If your ul has a class or ID, you can do something like this:

$('#myList > li')
JasCav
Down-voters should be decent, and give a reason. From the [faq](http://stackoverflow.com/faq) *"If you see misinformation, vote it down. Add comments indicating what, specifically, is wrong."*
patrick dw
Voted back up to zero, why was this -1?
Deebster
@patrick - Agreed. If I am wrong, I would love to learn why so I can be right the next time I have to answer a question. @Deebster - Thank you for voting me back up. I appreciate it.
JasCav
The vote may have come from your (brief) original answer of `$('ul li')`, but an explanation should have been given, and it should have been removed after the answer was fixed.
patrick dw
A: 

The jQuery selector syntax for parent/child is:

$('ul > li')

The child combinator (E > F) can be thought of as a more specific form of the descendant combinator (E F) in that it selects only first-level descendants.

With the structure you have, you may need to specify the ul parent element as well.

Oded
Can the downvote please explain?
Oded
@Oded - Someone is getting downvote happy (see my response, too). +1 back up for you because I don't like downvotes without a reason.
JasCav
I didn't down-vote you, but this will give you the child `<li>` elements of *every* `<ul>`, including the nested one(s). You would need to add a specifier to indicate only the outer-most `<ul>`. [This example](http://jsfiddle.net/dEhC4/) will alert `6` instead of `4`, indicating that all `<li>` elements have been selected.
patrick dw
+1  A: 

You would need the element or class which is above the first ul, then specify {parent-selector} > ul > li.

ClarkeyBoy
I like this because it down not require me to change the ul menu at all such as adding a class to it. This menu is automatically generated so I do not have much control over it. So wrapping it i a div with a class works perfectly. Thanks!
TruMan1
+1  A: 

Hi,

A quick fix, if possible would be to add a class name on the outer ul and select with $('ul.myClassName > li').

Another option: $('ul > li:has(ul)') it will give you all but the last nesting level.

Alin Purcaru
+1  A: 
$(".analytics:parent > li")

Will work for this specific case, but you are better off giving the top level a class or id.

Something like:

$("#top-level-ul > li")

Seems easier to follow than having to select the parent of one of your lis

sworoc
A: 

$('ul li:first')

Hardwareguy
This will select the first li tag in the list, not the top level in nested li's. Perhaps a misunderstanding of the question?
sworoc
My code will return every first child li of a ul, is that not what he's asking for?
Hardwareguy
@Hardware - I didn't down-vote you, but the OP wanted *all* the child `<li>` elements of only the outermost `<ul>`. That's what was meant by *first-level*. :o)
patrick dw
+1  A: 

$("ul:first > li").

MrAwesome
This is an excellent solution, but you must also consider other unordered-lists in the page. However, should work for the markup given, very creative.
sworoc