//eg1 - basic each loop
$.each($('.colors'), function(index, value) {
console.log(index + ':' + value);
//[object HTMLDivElement]
//value is an object reference to the DOM representation of the div element.
//use js/jQuery to do something useful with it - see below examples
});
//eg2 - turn into jQuery object (so it inherits jQuery API functions)
$('.colors').each(function(index, value) {
console.log($(value));
//console.log($(this)); //same result
});
//eg3 - get the single native DOM element not as an array
// Get the Nth element in the matched element set OR
// Get the whole matched element set as a clean array
$('.colors').each(function() {
console.log($(this).get(0));
});
console.log('--- variations -----------------------------------------');
//eg4 - messing around...
$('.colors').each(function() {
console.log($(this).html());
console.log($(this).eq(0).html());
console.log($(this).first().html());
});
console.log('--- types ----------------------------------------------');
//eg4 - messing around...(types)
$('.colors:first').each(function() {
console.log(typeof(this));
console.log(this.toString());
console.log(typeof($(this)));
console.log($(this).toString());
});
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.