Filter objects by className from nodeList

ClassicError picture ClassicError · Jan 20, 2017 · Viewed 9.1k times · Source

I've got a nodelist of objects returned by a call to document.getElementsByClassName('a'). Some of these objects have an extra class b. How can I create an array of elements that contain both classes? I don't was use a global search for b. I prefer vanillaJS/ES6 code.

HTML code example:

<div class ="a"></div>
<div class ="a"></div>
<div class ="a b"></div>
<div class ="a"></div>
<div class ="a b"></div>
<div class ="a"></div>

Answer

user2867288 picture user2867288 · Jan 20, 2017

Try querySelector instead.

document.querySelectorAll('.a.b');

This is likely your simplest solution.