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

165
Views
Mouse click event on a simple list: keep the style on, and all others turn off

I'm using a list to filter a number of other elements. Basically, I need help with making it show visually which one is the most recently selected item,

  • Current Status: My current mouseover, mouseout and click events make it possible for hover and click events to create the desired style formatting.
  • Desired Effect 1: When the user clicks a first item, and mouseouts, that item should remain formatted a certain way (it currently turns off because of mouseout).
  • Desired Effect 2: When the user clicks another subsequent item, that subsequent item should take on the formatting, and the first one should lose that formatting.

HTML

<div class="sidebar">
  <div class="heading">
    <h1>Select Country</h1>
  </div>
  <ul class="countryList">
    <li class="countryItem" id="australia">Australia</li>
    <li class="countryItem" id="france">France</li>
    <li class="countryItem" id="germany">Germany</li>
  </ul>
</div>

JS

// I'm using this 'items' bit to control a lot of other code so would prefer a solution which keeps it.
const items = document.getElementsByClassName('countryItem')
    console.log(Array.from(items))


// Some Mouse Events
    Array.from(items).forEach(function(item) {
        item.addEventListener('mouseover', function(handleMouseOver) {
            item.style.color = '#007dbc';
            item.style.fontWeight = "700";
        })
    })
    
    Array.from(items).forEach(function(item) {
        item.addEventListener('mouseout', function(handleMouseOut) {
            item.style.color = '#000';
            item.style.fontWeight = "400";      
        })
    })
    
    Array.from(items).forEach(function(item) {
        item.addEventListener('click', function(handleClick) {
            item.style.color = '#0007dbc';
            item.style.fontWeight = "700";      
        })
    })

CSS

ul {
    list-style-type: none;
    font-size:12px;
    margin: 0;
    padding: 20px;
}

li {
  cursor:pointer;
}

Thank you!

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

0

The easiest way to solve this problem is create a flag var to specify which item is highlighted.

const items = document.getElementsByClassName('countryItem')

let clickedItem = ''

Array.from(items).forEach(function(item) {
    item.addEventListener('mouseover', function(handleMouseOver) {
        item.style.color = '#007dbc';
        item.style.fontWeight = "700";
    })
})

Array.from(items).forEach(function(item) {
    item.addEventListener('mouseout', function(mouseOverEvent) {
        if (mouseOverEvent.target.id === clickedItem) return;
        item.style.color = '#000';
        item.style.fontWeight = "400";      
    })
})

Array.from(items).forEach(function(item) {
    item.addEventListener('click', function(clicnEvent) {
        if (clickedItem) {
          const prevItem = document.getElementById(clickedItem)
          prevItem.style.color = '#000';
          prevItem.style.fontWeight = "400";
        }
        clickedItem = clicnEvent.target.id
        item.style.color = '#0007dbc';
        item.style.fontWeight = "700";      
    })
})
ul {
    list-style-type: none;
    font-size:12px;
    margin: 0;
    padding: 20px;
}

li {
  cursor:pointer;
}
<div class="sidebar">
  <div class="heading">
    <h1>Select Country</h1>
  </div>
  <ul class="countryList">
    <li class="countryItem" id="australia">Australia</li>
    <li class="countryItem" id="france">France</li>
    <li class="countryItem" id="germany">Germany</li>
  </ul>
</div>

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!