views:

35

answers:

3
<ul class="list">
    <li class="class1">text</li>
    <li class="class2">text</li>
    <li class="class3">text</li>
    <li class="class4">text</li>
    <li class="class5">text</li>
</ul>

How do I search for some class inside .list?

Like:

search for .class1 inside .list () {
     // do something if true
} else {
     // do something if false
}
+3  A: 

jQuery just uses CSS selectors. So use the descendant selector.

var $items = $('.list > .class1');

if ( $items.length ) {
  // do something here
} else {
  // if false
}
BBonifield
@Happy I'm not sure what you're asking there.
BBonifield
+1  A: 

To iterate over every element in ul.list:

$('.list').each(function() {
  //do something here
});

To access a more specific element you can use a more specific CSS selector:

$('.list > .class1').each(function() {
   //do something here
}

You can also assign it to a variable. The operations you perform on it may will logically depend on how many elements the selector contains.

//example
var listItem1 = $('.list > .class1');
listItem1.hide();
calvinf
@calvinf $('.list').each(... iterates over each .list element, not every element in ul.list. You're looking for $('.list > *') or $('.list').children()
BBonifield
You are correct, I missed that. I'd update it to $('ul.list > li').each(/*...*/);
calvinf
+1  A: 
var classOneEl = $('.list').find('.class1');

Or any of the other answers will work. Many ways.

Ryan