tags:

views:

74

answers:

3

hi guys, i wonder how i can determine if a ul has MORE than 2 children and if there are two children with two specific classes inside this ul …

if($(this).children().length > 2 && $(this).children('.section-title, .active')) {
    $(this).append('<li class="dots">&hellip;</li>');
}

???

+1  A: 
var ulChildren = $("li", this);
if (ulChildren.length > 2 && $('li.section-title', ulChildren).length >= 1 && $('li.active', ulChildren).length >= 1)

This will check the following rules:

  1. There are more than two li elements under the ul
  2. There is at least one li with the class of section-title
  3. There is at least one li with the class of active
spinon
the description of my problem is rather difficult for me ;)if a ul has three li's in it, at least two of them must have the mentioned classnames. so there must be all three criterias fullfilled.
ok so let me tweak my answer then a little because this isn't exactly correct. The above would be close but it would pass if there were 3 active li members in the ul and no section-title which it sounds like is not what you are looking for. So let me change to make sure there is at least one section-title and one active
spinon
Why not just `var ulChildren = $(this).children('li')` ? Btw, you are missing quotation marks. And it probably should be `ulChildren.length > 2`.
Felix Kling
yeah good catch I am missing the quotation marks. Why use .children when you can just the selector for children. Has the same effect except this is only pulling the li objects that are children. Obviosuly in ul there isn't much chance that there would be any children. But with div you could have all kinds of different elements as children and this way you can limit to the tag you want.
spinon
+1  A: 
var $ul = $('ul');
if($ul.find("li").length > 2 && $ul.find('.active, .inactive').length  == 2) {
       alert('yes, it is this way');
}​

<ul>
  <li class="active">Whatever</li>
  <li class="inactive">Whatever</li>
  <li>Whatever</li>
  <li>Whatever</li>
</ul>​

Demo: http://jsfiddle.net/RtTSM/1/

karim79
A: 
var ul = $('#ul_id');
if ($('.class1, .class2', ul).size()>2)

you don't need to test the first condition (has more than 2 children), since it's an "AND" condition, and if your second condition satisfies, the first condition is trivial.

James Lin

[email protected]

James Lin