Aminur Rahman
Aminur Rahman

Reputation: 17

How to select all elements of body except one in javascript

<body>.
    <section class="apply-me1">.

    </section>.

    <section class="apply-me2">.

    </section>.

    <a class="except-me" href="#">Something here</a>.
</body>

Now I want to apply some rules on all elements except the a tag using css and javascript not jquery.

I Tried this:

CSS:

body {
    some rules here
}

body >*:not(.except-me).active {
    some rules here
}

Javascript:

selectBody = document.getElementsByTagName('BODY')[0];

selectBody.addEventListener('click', () => {

    selectBody.classList.toggle('active');

})

Basically I want to switch classes on body so some rules can be applied on all elements except the a tag when I click on body.

How can I do it ?

Upvotes: 0

Views: 1672

Answers (1)

Amini
Amini

Reputation: 1792

You can simply use querySelector(). What you must pass in the braces is a css selector. a sample here :

document.querySelector('body >*:not(.except-me).active');

This will help you a lot. Don't forget that you MUST put a css selector in the braces.

Upvotes: 1

Related Questions