Get item by tag name and class name

(in vanilla JavaScript) I was wondering if there was an easy way to do something like

x = document.getElementsByTagName('span') && getElementsByClassName('null'); 

To return all 'span' elements that have class name 'null'?

I thought it could be something like:

 x = document.getElementsByTagName('span'); x = x.getElementsByClassName('null'); // or x = document.getElementsByTagName('span').getElementsByClassName('null'); 

But that didn't seem to work.

Is this possible, or will I have to iterate over x, invoking everything that returns false for .class = 'null'?

Thanks.

+6
source share
1 answer

The DOM does not provide an API for filtering NodeLists.

Instead, you can use the CSS selector:

 var x = document.querySelectorAll('span.null'); 
+15
source

Source: https://habr.com/ru/post/951758/


All Articles