Pages

27 Aug 2013

jQuery.each() Class Example using Jquery

This example shows you how to loop through each element with class=”productDesc” given in the HTML below.


<div class=”productDesc” >Red</div>
<div class=”productDesc” >Orange</div>
<div class=”productDesc” >Green</div>


$.each($('.productDesc'), function(index, value) { 
    console.log(index + ':' + value); 
});
//outputs: 1:Red 2:Orange 3:Green


You don’t have to include index and value these are just parameters which help determine which DOM element your currently iterating. You could also write it like this:

$.each($('.productDesc'), function() { 
    console.log($(this).html());
});
//outputs: Red Orange Green

No comments:

Post a Comment