

Let us see how we can retrieve this element with the help of an example. Most of the time, there is a necessity to retrieve only the first element of the DOM matching the specified class and if its obtained or is present then performing specific manipulations on the same.

Getting the first element matching the specified class else undefined. And output of the code will be as follows : Var myElements = document.getElementsB圜lassName('sample demo') Īfter clicking on “Change background color of elements with sample and demo class” button only the elements with classes having both sample and demo will be retrieved. Now let us learn how we can retrieve the elements of all the DOM with classes specified more than one in the parameter of the getElementsB圜lass() method.ĭemonstration of getting elements of a multiple class in javascriptĬhange background color of elements with sample or demo class Javascript Get Element By Attribute JavaScript Get Element By Attribute In this tutorial, you will learn how Javascript get element by attribute of the element. Return value A string containing the value of the key. Syntax getItem(keyName) Parameters keyName A string containing the name of the key you want to retrieve the value of. Var myElements = document.getElementsB圜lassName("sample") for(var counter = 0 counter Īfter clicking on the “Change background color of elements with sample class” button output is as follows –ĭocument Scope with Multiple Classes. The getItem () method of the Storage interface, when passed a key name, will return that keys value, or null if the key does not exist, in the given Storage object. Let us change the background color of all the elements of the DOM who have sample class to pink.Ĭhange background color of elements with sample class Demonstration of getting elements of a particular class in javascriptĮDUCBA is the best platform to learn languages.
