Leke
Leke

Reputation: 883

Get element by tag name and class name

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

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

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

I thought it might have been something like:

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

But that didn't seem to work out.

Is this possible or will I have to iterate through x popping anything that returns false for .class='null'?

Thanks.

Upvotes: 13

Views: 22723

Answers (1)

SLaks
SLaks

Reputation: 887453

The DOM does not provide any APIs for filtering NodeLists.

Instead, you can use CSS selectors:

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

Upvotes: 23

Related Questions