views:

97

answers:

3

I want to select list items that are immediate children of #nav, that do not themselves have immediate children with an 'active' class.

This is what I think it should be but it does not work:

$('#nav > li:not(> a.active)')
+1  A: 

For this jquery has the not selector

you can do something like

$("#nav > li").children(":not(.active)");
Jeremy B.
This is not what Trevor is looking for. He wants **children of #nav, that do not themselves have immediate children with an 'active' class**.
patrick dw
missed one of the children in there, fixed that.
Jeremy B.
The elements he wants to select are the li elements. Your selector would return their children.
Ken Browning
Good point, the wording is a bit challenging but I see what he's looking for, will look back at that again.
Jeremy B.
+1  A: 

I really like Ken's solution, but just for an alternative take.

You could add the active class to your list items instead of your links inside. Then your selector could look like:

$("ul#nav li:not(.active)");

If you want to style the links based on the active class, your CSS could look like this:

#nav li.active a{background-color:red;}
Jon
Why has this answer been downvoted twice? If it is incorrect, which part does not work and how can I improve it?
Jon
This answer is incorrect because it returns the children of `li`. Trevor wants the `li` elements themselves. Re-read the question.
patrick dw
Thank you! I missed that part, I will change my answer to reflect that.
Jon
+1  A: 

You'll need to use jQuery's filter function:

$('#nav > li').filter(function() { return !($(this).children().is('.active')); })
Ken Browning
This is even better than your first solution. I think you should have kept that one around though as an alternate. Or was there a problem with it?
patrick dw
No, this one seems to work intermittently. I'm reverting back to previous answer.
Ken Browning
This will return li elements that don't have active, I don't believe it will check the children of the li elements for the active class.
Jeremy B.
I think **Jeremy B.** is right. The 'selector only' solution doesn't seem to test against the children, but rather the `li` only.
patrick dw
I agree. Taking out the selector-only answer and leaving `filter`
Ken Browning
@Ken: Good call. I was going to offer an answer that used `each()`, but `filter()` is a *much* nicer solution.
patrick dw
Thanks Ken, this is perfect...and thanks everyone else for their help!
Trevor
@Trevor, glad you found your answer and welcome to Stackoverflow! Please click on the check mark by Ken's answer so he gets credit for the solution.
Jon