For example, in jQuery, if i want all <div>
and <p>
elements, I do this:
var $x = $("p, div");
And then if i want all the <div>
elements in x, then I do this:
var $divs = $x.filter("div");
So how do i do this simple filter
thing in vanilla JavaScript?
For example, to select all <div>
and <p>
, then I can do this:
var x = document.querySelectorAll("div, p");
But vanilla JavaScript doesn't have the filter function like in jQuery, so I can't do this:
var divs = x.filter("div"); // ERROR
Hope someone can help me with this :-)
Thanks in advance.
UPDATE
Some comments/answers have suggested to do something like .tagName == "DIV" to find the divs, however, i want a solution with a string selector like in jQuery.
The reason is because i also want to filter with attributes, classes and even multiple selectors where you put in comma. And the string selector must be dynamic.
That means, i dont know whats in the selector. It could be "div[foo='asd'], .bar" or "#hello, [xx='yy'], p"
So i cant just hardcode the .tagName == "DIV", beacuse i dont know whats in the selector string.