4

Say I got something like this:

function one() {
    var findthemall = document.querySelectorAll("*");
    var i;
    for (i = 0; i < findthemall.length; i++) {
        //doin' some cool stuff
    }
}

Now, I know I can list more than one tag in querySelectorAll using comma between them, but is there a simple way to make an exception for some specific classes/tags?

Like: querySelectorAll("*, but not p and br tags")

franenos
  • 337
  • 2
  • 5
  • 14

2 Answers2

6

Yes, the method .querySelectorAll() accepts CSS3 selectors, which means that you can use the :not() pseudo class to negate certain elements.

In this case, the selector *:not(p):not(br) would negate br and p elements.

document.querySelectorAll("*:not(p):not(br)");
Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
2

https://developer.mozilla.org/en-US/docs/Web/CSS/:not

document.querySelectorAll("*:not(p):not(br)")
Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217
  • 1
    @squint Thanks, I was testing it as you commented – Ruan Mendes Feb 01 '16 at 18:23
  • 1
    Related to your first edit: http://stackoverflow.com/questions/10711730/why-is-my-jquery-not-selector-not-working-in-css The `:not()` pseudo class only accepts simple selectors.. I think `*:not(br, p)` would work in jQuery, but that's because it uses sizzle under the hood if a simple selector isn't supplied. – Josh Crozier Feb 01 '16 at 18:29