HTML DOM getElementsByClassName() Method

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP








googletag.cmd.push(function() googletag.display('div-gpt-ad-1422003450156-2'); );




HTML DOM getElementsByClassName() Method


❮ Document Object




Example


Get all elements with the specified class name:



var x =
document.getElementsByClassName("example");

Try it Yourself »

More "Try it Yourself" examples below.



Definition and Usage


The getElementsByClassName() method returns a collection of all elements in the document with the specified
class name, as a NodeList object.


The NodeList object represents a collection of nodes. The nodes can be accessed by index numbers. The index starts at 0.


Tip: You can use the
length property of the NodeList object to determine the number of elements
with a specified class name, then you can loop through all elements and extract
the info you want.



Browser Support


The numbers in the table specifies the first browser version that fully
supports the method.














Method
getElementsByClassName() 4.0 9.0 3.0 3.1 9.5

Syntax



document.getElementsByClassName(classname)


Parameter Values








ParameterType Description
classnameStringRequired. The class name of the elements you want to get.

To search for
multiple class names, separate them with spaces, like "test demo".






googletag.cmd.push(function() googletag.display('div-gpt-ad-1493883843099-0'); );





Technical Details






DOM Version: Core Level 1 Document Object
Return Value: A NodeList object, representing a collection of elements with the specified class name. The elements in the
returned collection are sorted as they appear in the source code.

More Examples




Example


Get all elements with both the "example" and "color" classes:



var x =
document.getElementsByClassName("example color");

Try it Yourself »



Example


Find out how many elements with class="example" there are in the document (using the length
property of the NodeList object):



var x =
document.getElementsByClassName("example").length;

Try it Yourself »



Example


Change the background color of all elements with class="example":



var x = document.getElementsByClassName("example");
var i;
for (i = 0; i < x.length; i++)

  x[i].style.backgroundColor = "red";

Try it Yourself »


Related Pages


CSS Tutorial: CSS Syntax


CSS Reference: CSS .class Selector


HTML DOM Reference: element.getElementsByClassName()


HTML DOM Reference: className Property


HTML DOM Reference: classList Property


HTML DOM Reference: Style Object



❮ Document Object


Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)