Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery: if children with specific class?

Tags:

jquery

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>');
}

???

like image 560
matt Avatar asked Jul 04 '10 20:07

matt


People also ask

How do you check if an element has a child with a class?

Use the querySelector() method to check if an element has a child with a specific id, e.g. if (box. querySelector('#child-3') !== null) {} . The querySelector method returns the first element that matches the provided selector or null of no element matches.

How do you check if an element contains a class in jQuery?

jQuery hasClass() Method The hasClass() method checks if any of the selected elements have a specified class name. If ANY of the selected elements has the specified class name, this method will return "true".

What is the use of parent () and child () method in jQuery?

It is a jQuery Selector used to select all elements that are the direct child of its parent element. Parameter Values: parent: Using this, the parent element will be selected. child: Using this, the direct child element of the specified parent element will be selected.

How can I get second child in jQuery?

grab the second child: $(t). children(). eq(1);


2 Answers

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/

like image 81
karim79 Avatar answered Sep 18 '22 07:09

karim79


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
like image 38
spinon Avatar answered Sep 20 '22 07:09

spinon