1

So this is my current jquery code, which I would like in vanilla js.

var elems = [];

$("*").not('script, style, iframe').each(function() {
    elems.push($(this)[0]);
});

the closest alternative I found was document.getElementsByTagName("*") but that still has iframe, style, and script tags, which I don't want in my array.

Also, I can't just remove them by their tag name specifically, as they might have an id or a class associated with them.

1 Answer 1

5

One option would be to use a CSS selector along with the .querySelectorAll() method.

Since CSS has a :not() pseudo-class, you could use that to negate the script, style, and iframe elements.

var elements = document.querySelectorAll('*:not(script):not(style):not(iframe');

In the snippet above, elements would be a NodeList containing a collection of the selected elements.

Sign up to request clarification or add additional context in comments.

2 Comments

That worked perfectly! I'll mark as correct as soon as stackoverflow allows me to, Thanks a bunch!
Yes, that is what I was using anyway, a nodelist.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.