views:

21

answers:

2

hallo all. i am trieng to build a function that gets all ul's on screen that have less then 3 li elements. i need this as a jquery object. so i dont mind if it will be a function or just one selector.

how do i do it?

thanks

+3  A: 

You can use .filter() for this, for example:

$("ul").filter(function() { return $(this).children().length<3; }).doSomething();

With this we're getting all <ul> elements, then filtering down to only those who have fewer than 3 children, by checking the .length of .children().

Nick Craver
i dare to give alternative answer in front of Nick :)
JapanPro
@JapanPro - alternatives are the spice of life, always post em of you got em.
Nick Craver
A: 

I believe this will work:

$('ul:not(:contains(li:eq(2)))')
MightyE
`:contains()` is a text selector, you probably wanted `:has()` here, but that'd still give false positives with nested lists.
Nick Craver