loop through html elements javascript code example

Example 1: loop elements in javascript

var list = [{a:1,b:2}, {a:3,b:5}, {a:8,b:2}, {a:4,b:1}, {a:0,b:8}];

for (var i = list.length - 1, item; item = list[i]; i--) {
  console.log("Looping: index ", i, "item", item);
}

######### ES6 Update ################

for (let item of list) {
    console.log("Looping: index ", "Sorry!!!", "item" + item);
}

Example 2: loop through dom elements javascript

//Loop thru DOM elements

var all = document.getElementsByTagName("*");

for (var i=0, max=all.length; i < max; i++) {
     // Do something with the element here
}

Example 3: how to loop over dom objects javascript

Array.from($('.'+$( this ).attr('id'))).forEach(function(obj){

    console.log(obj);
  });