Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery loop through Child divs

<div id="ChosenCategory" class="chosen">
   <div class="cat_ch" name="1">
   <div class="cat_ch" name="2">
   <div class="cat_ch" name="3">
   <div class="cat_ch" name="5">
   <div class="clear"> </div>
</div>

I want to loop though div.cat_ch How?

This one fails:

    $("div").each(function () {
       alert("FW");
       alert($(this).attr("name").val());
    });
like image 903
levi Avatar asked Jul 23 '12 19:07

levi


1 Answers

$('#ChosenCategory').children('.cat_ch').each(function() {

});

Or

$('#ChosenCategory > .cat_ch').each(function() {

});

JQuery's .children method and css3 child selector > will return only the direct children that match the selector, class .cat_ch in the example.

If you want to search deeper in the DOM tree, that is, include nested elements, use .find or omit the child selector:

$('#ChosenCategory').find('.cat_ch').each( function(){} )

Or

$('#ChosenCategory .cat_ch').each( function(){} )
like image 198
Ortiga Avatar answered Oct 04 '22 21:10

Ortiga