how to use each function in jquery

sam
sam
Member
378 Points
48 Posts

Hi,

how to use each function in jquery?

Views: 8802
Total Answered: 2
Total Marked As Answer: 1
Posted On: 14-May-2015 03:22

Share:   fb twitter linkedin
Answers
avnish
avnish
Participant
242 Points
11 Posts
         

jQuery each function is used to iterate over the items in a collection. For each item in the collection the anonymous function is called. The index of the element and the element itself are passed to the anonymous function. 

$('li').each(function (index, element) {
    alert(index + ' : ' + $(element).text());
});

To refer to the current element that we are iterating over you can also use this keyword.
$('li').each(function (index) {
    alert(index + ' : ' + $(this).text());
});

If you don't care about the index and just need the text of the list item, then you can get rid of the index parameter
$('li').each(function () {
    alert($(this).text());
});

Posted On: 14-May-2015 01:26
sam
sam
Member
378 Points
48 Posts
         

Hi Avnish,

It working...

 

Posted On: 14-May-2015 09:47
 Log In to Chat