Skip to content

Instantly share code, notes, and snippets.

@kevinchisholm
Last active January 20, 2018 17:15
Show Gist options
  • Save kevinchisholm/26d79556dade79be6c61f0dd568779ad to your computer and use it in GitHub Desktop.
Save kevinchisholm/26d79556dade79be6c61f0dd568779ad to your computer and use it in GitHub Desktop.
Iterating Over a DOM Collection with the jQuery.each Method
$(selector).each(function(){
//whatever you put in this function will
//happen to every element in the collection
});
$("ul li").each(function(){
$(this).click(function(){
$(this).css("color","red");
});
});
$("ul li.workday").each(function(){
$(this).click(function(){
$(this).css("color","red");
});
});
$('a');
$('ul.sales li');
$('div.members div');
<ul>
<li class="workday">Monday</li>
<li class="workday">Tuesday</li>
<li class="workday">Wednesday</li>
<li class="workday">Thursday</li>
<li class="workday">Friday</li>
<li>Saturday</li>
<li>Sunday</li>
<ul>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment