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

125
Views
Change color of current selected element sidebar

I have a sidebar with site pages. I want to change the color of a currently selected page when clicked, assign the class is-active to the currently selected item, and remove it from all the others. Here is the HTML:

<aside id="side-bar" class="sidebar">
        <h3>Pages</h3>
        <nav class="menu">
            <a onclick="changeColor()"  class="menu-item is-active">page 1</a>
            <a onclick="changeColor()"  class="menu-item">page 2</a>
            <a onclick="changeColor()"  class="menu-item">page 3</a>
        </nav>
</aside>

And CSS:

.sidebar .menu .menu-item:hover,
.sidebar .menu .menu-item.is-active {
    color: #f1672c;
    border-right: 6px solid #f1672c;
}

Each element has the function change color that should take all the elements, remove the is-active class from previous selected element and add to the curent clicked element.

function changeColor(){
    var links = document.getElementsByClassName("menu-item")
    links.map(classList.remove("is-active"))
    this.classList.add("is-active")
}

What am I missing?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

That changeColor() is not necessary....

const pages = document.querySelectorAll(".menu-item");

pages.forEach((item) => {
  item.addEventListener('click', active_item);
})

function active_item () {
  pages.forEach((item) => {
    item.classList.remove('is-active');
  });
  this.classList.add('is-active');
}
.is-active {
  background-color: red;
}
<aside id="side-bar" class="sidebar">
        <h3>Pages</h3>
        <nav class="menu">
            <a class="menu-item is-active">page 1</a>
            <a class="menu-item">page 2</a>
            <a class="menu-item">page 3</a>
        </nav>
</aside>

about 4 years ago · Juan Pablo Isaza Report

0

Just pass the reference of current element with this.

function changeColor(el){
    document.querySelector(".menu-item.is-active").classList.remove("is-active")
    el.classList.add("is-active")
}
.sidebar .menu .menu-item:hover,
.sidebar .menu .menu-item.is-active {
    color: #f1672c;
    border-right: 6px solid #f1672c;
}
<aside id="side-bar" class="sidebar">
        <h3>Pages</h3>
        <nav class="menu">
            <a onclick="changeColor(this)"  class="menu-item is-active">page 1</a>
            <a onclick="changeColor(this)"  class="menu-item">page 2</a>
            <a onclick="changeColor(this)"  class="menu-item">page 3</a>
        </nav>
</aside>

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!