Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get elements by class name on HTMLCollection [duplicate]

Tags:

javascript

I've been thinking about why we cannot do this in javascript.

document.getElementsByClassName('class1').getElementsByClassName('class2')

This will not work, because the first getElementsByClassName call will give us an HTMLCollection, which doesn't have getElementsByClassName defined on it. Why is this? This would be a great function to use in this way, since it would let you get elements based on them having multiple different classes, not just one.

Is there any way to:

  1. Get elements by class name in HTMLCollection
  2. Get elements by multiple class names
like image 686
Sahand Avatar asked Jan 03 '18 16:01

Sahand


People also ask

How do you traverse through the elements selected by get elements by class name method?

Iterate Through Elements Returned by getElementsByClassName getElementsByClassName method is to use the for-of loop. We call document. getElementsByClassName with 'slide' to return an HTML element collection object with all the divs. Then we use the for-of loop to loop through each item.

How do you query HTMLCollection?

HTMLCollection items can be accessed by their name, id, or index number. NodeList items can only be accessed by their index number. An HTMLCollection is always a live collection. Example: If you add a <li> element to a list in the DOM, the list in the HTMLCollection will also change.

What is used to find all HTML elements with the same class name?

getElementsByClassName() 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.


2 Answers

Get by multiple class names:

document.querySelector('.someclass.otherclass'); // get first element that matches
document.querySelectorAll('.someclass.otherclass'); //get all elements that match

You can querySelector* any DOM element, not just document, in order to search in it.

Get by class name in HTMLCollection

[].filter.call(
    htmlCollection, element => [].includes.call(elements.classList, 'someclassname')
)

Neither HTMLCollection nor .classList are arrays, only array-like objects, hence the need for .call.

like image 52
kon.simeonov Avatar answered Oct 25 '22 04:10

kon.simeonov


  1. Get elements by class name in HTMLCollection :

    HTMLCollection.prototype.forEach = Array.prototype.forEach;
    
    HTMLCollection.prototype.getElementsByClassName = function( name ){
        var all = [];
        this.forEach( function( el ){
             if( el )
                 all.concat( el.getElementsByClassName( name ) );
        });
        return all;
    }
    
  2. Get elements by multiple class names :

    document.querySelectorAll('.class1.class2');
    

Please use the second for better performance.

like image 41
karkael Avatar answered Oct 25 '22 03:10

karkael