// document.getElementById()
console.info("document.getElementById()");
var myName = document.getElementById("name");
console.log(myName.innerHTML);
// document.getElementsByClassName
console.log( "\n" );
console.info("document|element.getElementsByClassName()");
var bluesGuitarPlayers = document.getElementsByClassName('blues')
var rockGuitarPlayers = document.getElementsByClassName('rock')
console.log(rockGuitarPlayers);
console.log( "---" );
// or to restrict the search in an element...
var myList = document.getElementById("guitar-players");
var myBluesGuitarPlayers = myList.getElementsByClassName('blues')
var myRockGuitarPlayers = myList.getElementsByClassName('rock')
console.log(myRockGuitarPlayers);
// won't work as an HTMLCollection is not exactly an array
/*
myRockGuitarPlayers.forEach(...)
*/
console.log( "---" );
console.log( myRockGuitarPlayers[0].innerHTML )
console.log( myRockGuitarPlayers.item(0).innerHTML )
console.log( myRockGuitarPlayers.namedItem("jimi").innerHTML )
console.log( "---" );
// ...but it has a length property
for (var i = 0; i < myRockGuitarPlayers.length; ++i) {
var link = myRockGuitarPlayers[i];
console.log(link.innerHTML);
}
// document.getElementsByTagName
console.log( "\n" );
console.info("document|element.getElementsByTagName()");
var links = document.getElementsByTagName('a')
// or to restrict the search in an element...
var myList = document.getElementById("guitar-players");
var linksGuitarPlayers = myList.getElementsByTagName('a')
console.log( linksGuitarPlayers[0].href )
console.log( linksGuitarPlayers.item(0).href )
console.log( linksGuitarPlayers.namedItem("clapton").href )
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.