3

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
  • 287
  • 3
  • 11

2 Answers2

5

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
  • 219,308
  • 53
  • 366
  • 287
2

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

document.querySelectorAll("*:not(p):not(br)")
Juan Mendes
  • 85,853
  • 29
  • 146
  • 204
  • 1
    @squint Thanks, I was testing it as you commented – Juan 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