532 Show
Learn how to get one or more HTML element objects by class name using getElementsByClassName() and querySelectorAll() methods in JavaScript.
1. Get Element By Class Name Using getElementsByClassName()Create five div elements with the same class name box.
Get all HTML element objects that have box class in them by running getElementsByClassName() method on the document object.
The getElementsByClassName() method takes one argument which is the value of the class attribute. In this case box.
Once the getElementsByClassName() method finds one or more elements, it will return them as HTMLCollection. HTMLCollection is an array-like object and it has the length property that returns a number of items in it.
Loop through the HTMLCollection using for…of to get each element inside it.
Try it outSee the Pen Get Element By Class Name #1 by SoftAuthor (@softauthor) on CodePen. 2. Get Element By Class Name Using querySelectorAll()Alternatively, the querySelectorAll() method also gets one or more elements by class name. Call the querySelectorAll() method on the document object. The querySelectorAll() method takes one argument which is the class name similar to the getElementsByClassName() method. Unlike argument in getElementsByClassName(), the class name in the argument of the querySelectAll() must be prefixed with dot (.)
It returns NodeList which is a collection of document nodes. NodeList can be iterated by the forEach() method or for…of loop.
Try it outSee the Pen Get Element By Class Name #2 by SoftAuthor (@softauthor) on CodePen. 3. Get Element By Multiple Class NamesLet’s see how to get one or more elements that have more than one class name.
Get HTML elements that have two classes which are box and red. To do that, invoke the getElementsByClassName() method on the document object. It takes one argument, which is class names box and red separated by a space in quotes.
Try it outSee the Pen Get Element By Class Name #2 by SoftAuthor (@softauthor) on CodePen. 4. Get Elements By Class Name From Parent ElementLearn how to get elements by class name from a parent element.
Get the parent element by calling the getElementById() method on the document object. Assign it to a constant boxContainer. Then, invoke the getElementsByClassName() on the boxContainer, which is the parent element.
Try it outSee the Pen Get Element By Class Name #4 by SoftAuthor (@softauthor) on CodePen. Can I get element by class in JavaScript?The JavaScript getElementsByClassName is used to get all the elements that belong to a particular class. When the JavaScript get element by class name method is called on the document object, it searches the complete document, including the root nodes, and returns an array containing all the elements.
What is Getelementbyclass in JavaScript?The getElementsByClassName method of Document interface returns an array-like object of all child elements which have all of the given class name(s). When called on the document object, the complete document is searched, including the root node.
What does getElementsByClassName () function return?The getElementsByClassName() method returns a collection of elements with a specified class name(s). The getElementsByClassName() method returns an HTMLCollection.
What is difference between getElementById and getElementsByClassName?We want to get the unique element and allocate it in a variable this can be done by making use of getElementById. But when we want to get all the products elements and allocate them in a variable then basically we are using getElementByClassName.
|