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

241
Views
How to select all elements and remove a CSS class

I have the following JS function which on click is making the chosen element to be selected(is changing the background color) and should make all other elements to remove this class. However, for some reason onClick the "menu-category-hover" class is not removed.

function GetData(allMenuItems, theCategoryId) {
    
    // Remove selected class on other Elements
    allMenuEl = document.querySelectorAll('menu-category');
    for (var i = 0; i < allMenuEl; i++) {
        allMenuEl[i].classList.remove("menu-category-hover");
    }

    .....

    // On element Click
    var hoverEl = document.getElementById("menu_cat_" + theCategoryId);
    hoverEl.classList.add("menu-category-hover");
}

<div class="menu-category menu-category-hover" onclick="GetData(, 13)" id="menu_cat_13">
     <img src="/images/85783_burger.png" class="menu-home-image-img">
     <p class="menu-home-name">Burgers</p>
<div>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Check this

function GetData(allMenuItems, theCategoryId) {
    
    // Remove selected class on other Elements 
    allMenuEl = document.querySelectorAll('.menu-category'); //<--- class indicator `.`
    for (var i = 0; i < allMenuEl.length; i++) { //<-- not added allMenuEl.length
        allMenuEl[i].classList.remove("menu-category-hover");
    }

    .....

    // On element Click
    var hoverEl = document.getElementById("menu_cat_" + theCategoryId);
    hoverEl.classList.add("menu-category-hover");
}

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!