Search code examples
javascriptselectors-api

adding an exception for querySelectorAll


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")


Solution

  • 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)");