Victor Lee
{C4}
February 23, 2015
I. What is jQuery?
II. What is .each()?
a) $.each() objects and arrays
b) $(selector).each() jQuery object
III. Summary
$.each()
$(selector).each()
It's the jQuery equivalent of javascript's loop function.
the $.each() function is used to iterate over any collection, whether it is an object or an array
$.each(collection, callback(index, value);
If an object is used as the collection, the callback is passed a key-value pair each time:
$("li").click(function(){
$('li').each(function(){
$(this).toggleClass("makeitalic");
});
});
1. $('li') is the jQuery selector for the <li> elements
2. .each(function) applies the same code to each element in the selection
3. $(this) refers to the current element <li> and will change the list style
example
iterate <li> to change the element styles