parm530
6/25/2018 - 3:12 PM

jQuery Iterators

iterating over jQuery objects

Iterators

  • Iterating over plain javascript array
  • $.each accepts 2 parameters: the array to iterate over and an anonymous function
    • The anonymous function accepts 2 params also: the first one is the index and the other is the element variable that is yeilded:
var namesArray = ['bob', 'mary', 'alex'];
$.each(namesArray, function(index, name) {
  //code
})
  • $().each is different because this iteration works only on jQuery objects!!
$('#div_name').each(function(index, element) {
  //code
});