Iterating over elements in JQuery

Posted on 15-02-2013 20:46 by graham
This tutorial describes how to iterate over elements of some collection in JQuery.

Iterating over elements of collection


To iterate over elements of a collection in JQuery and perform some action for each of them, use the each() function:
$("div").each(function() {
// this will be executed for every DIV element
});

Within the each() function, the current element which is being iterated is available in the variable $(this):
$("tr").each(function() {
// hide every row in the table
$(this).hide();
});

See demo and play with it in our code sandbox
Comments

 

Add comment

Has this tutorial been helpful to you? Or do you see anything wrong? We appreciate your opinion!
Your comment:
Show formatting hints
HTML is disallowed, but in your text you can use the following markup
  • [code][/code] for a block of code
  • [tt][/tt] for inline code
  • [link]link href|link anchor[/link] for links
  • [b][/b] for bold text
Email:
+ Ask a question
If you have a technical question related to programming and computers, ask it here. Other users will help you solve it!
Unanswered questions
Share your knowledge by helping others solve their problems