Pages

Thursday, December 17, 2015

each() function in Jquery - To Iterate each element over a collection

.each() function:   This function is used to iterate over the collections.
This function is same working as foreach function in asp.net.

Syntax:
 $(selector).each(callback function(){
});
Example:  Iterate over each row of a table.
$("table tr").each(function () {

     alert($(this).text());

});

You can also pass the parameters into the callback functions:
Syntax:
$(selector).each(callback function(index,element){
});
Index will give you the index of the current item.
Element will give you current element.

Example:

$(document).ready(function () {

     $("table tr").each(function (index, element) {

         alert($(element).text() + " at index: " + index);

     });


 });


Working of nested .each function:







No comments:

Post a Comment