How to find whether all element contains same class or not?
- Since the querySelectorAll() method returns a node list of all the selected elements, we first need to convert this node list into an array using the Array.prototype.from() method so that we can call the every() method on it.
- Finally, we will use the every() method on the returned array of the selected elements which would return true only if all the elements of the array have the specified class.
Example: In the following example, we have six-button elements in the HTML document, out of which only five have the “active” CSS class added to them.
If you add the “active” CSS class to all the buttons, the following output is shown.