Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

120
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!