2

I am trying to remove all classes except the fist one.

html:

<div class="note">1</div>
<div class="note">1</div>
<div class="note">1</div>
<div class="note">1</div>

Js:

for (var item of document.querySelectorAll("div.note(not:first-of-type"))) {
    item.classList.remove('note');
}

3 Answers 3

6

Use :not(:first-of-type):

for (var item of document.querySelectorAll("div.note:not(:first-of-type)")) {
    item.classList.remove('note');
}
.note {
  color: yellow;
}
<div class="note">1</div>
<div class="note">2</div>
<div class="note">3</div>
<div class="note">4</div>

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

Comments

1

Just loop and check the index like so:

Array.from(document.querySelectorAll("div.note")).forEach((div, ind) => {
    if (ind != 0) {
        div.classList.remove("note");
    }
});

Comments

1

you can simply use for loop also:

var array = document.querySelectorAll("div.note");
for(let i =1; i<array.length; i++){
    array[i].classList.remove('note')
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.