Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

123
Visualizações
Remove class from all element on webpage

I am beginner web developer. I have small problem with remove class from all elements on my webpage.

I have this code:

showErrorMessage(message, className) {
      let elems = document.querySelectorAll("text-danger invalid");
      [].forEach.call(elems, function(el) {
        el.classList.remove("text-danger invalid");
      });

      document.querySelector(className).className += " text-danger invalid";
      Swal.fire(
        'Błąd',
        message,
        'error',
      );
    },

This code work fine, but not remove old class from div. I need:

  1. Remove from all elements on my webpage this class: text-danger invalid
  2. add class to selected element
  3. show message Swal

I have problem with 1.

How can I repair it?

Please help me :)

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You have 2 issues with your code:

  1. There's an issue with your selector passed in line 2. As Ivan suggest below:
document.querySelectorAll(".text-danger.invalid");
  1. element.classList.remove() accepts classes as separate strings (see the MDN Docs). Replace line 4 with:
el.classList.remove("text-danger", "invalid");

EDIT: Incorporated the suggestions from below comments

about 4 years ago · Juan Pablo Isaza Relatório

0

You have a number of issues with your code:

  1. querySelectorAll() uses CSS selectors. So if you need to choose all elements with two classes, you need to use ".class-one.class-two".
  2. classList.remove() does not allow spaces, so separate classes with a comma.
  3. You can simplify your forEach().

So here is the code that should work:

  const elems = document.querySelectorAll(".text-danger.invalid")

  elems.forEach((item) => {
    item.classList.remove("text-danger", "invalid")
  })

See also the code snippet

function remove() {
  let elems = document.querySelectorAll(".text-danger.invalid")
  elems.forEach((item) => {
    item.classList.remove("text-danger", "invalid")
  })
}
.text-danger.invalid {
  color: red;
}
<div class="text-danger invalid">Text</div>
<div class="text-danger invalid">Text</div>

<button onclick="remove()">Remove classes</button>

about 4 years ago · Juan Pablo Isaza Relatório

0

querySelectorAll is array-like, is not array

Use Array.from for convert it to array

const elems = document.querySelectorAll("text-danger invalid");

Array.from(elems).forEach(e => {
    e.classList.remove("text-danger", "invalid");
});

Notice: Separate classes in classList.remove

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda