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

175
Views
How to make the event listener only add CSS element on one item at a time (vanilla JavaScript)

1

^I would like to be able for the style to be enabled for only one at a time.

2

^I'm able to do this, which I don't want the user to be able to do.

So it's weirdly hard framing a question for what is possibly an easy solution. I basically have a list of build versions where I want the user to select one. When one of the versions are selected, it adds a border to the item to display that its clicked. However, with my code right now the user is able to select all 3 items and enable their CSS elements. I would like for the user to be able to only "activate" one item from the list.

HTML and CSS:

<ul class="listContents">
      
         <li><p><a href="#" class="links">Stable</a></p></li>
        <li><p><a href="#" class="links">Preview</a></p></li>
        <li><p><a href="#" class="links">LTS</a></p></li>
</ul>

<style>
.colorText {
        background-color: #58a7ed;
        color: white;
}
</style>

and the JS stuff:

const btn = document.querySelectorAll('.links');

for (let i = 0; i < btn.length; i++ ) {
    btn[i].addEventListener("click", function() {
        btn[i].classList.add('colorText')
    })
}

I really hope I made myself clear, I feel like I'm failing my English trying to word this right lol.

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

0

You can also use a forEach loop, accessing the clicked link using event.target

const btns = document.querySelectorAll('.links');

btns.forEach(btn => {
  btn.addEventListener('click', e => {
    // remove any existing active links
    btns.forEach(b => b.classList.remove('colorText'));
    // activate the clicked link
    e.target.classList.add('colorText');
  })
});
.colorText {
  background-color: #58a7ed;
  color: white;
}
<ul class="listContents">
  <li>
    <p><a href="#" class="links">Stable</a></p>
  </li>
  <li>
    <p><a href="#" class="links">Preview</a></p>
  </li>
  <li>
    <p><a href="#" class="links">LTS</a></p>
  </li>
</ul>

about 4 years ago · Juan Pablo Isaza Report

0

Just before you add the colorText class to the desired item, we can remove colorText from ALL of them, ensuring that only 1 at a time gets the class.

// the rest is the same...
btn[i].addEventListener("click", function() {
  // remove it from all:
  btn.forEach(function(item) {
    item.classList.remove('colorText');
  });
  // add it back to the desired one
  btn[i].classList.add('colorText')
})
about 4 years ago · Juan Pablo Isaza Report

0

you can also use simple for of

const btn = document.querySelectorAll(".links");

for (let bt of btn) {
  bt.addEventListener("click", (e) => {
    btn.forEach((b) => b.classList.remove("colorText"));
    e.target.classList.add("colorText");
  });
}

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!