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

203
Views
Toggle class on div when a specific element is hovered hover (and removed)

I have this javascript (below) which adds/removes a class when an anchor element is hovered over.

a.forEach(item => {
  item.addEventListener('mouseover', () => {
    cursor.classList.add('hover');
  });
  item.addEventListener('mouseleave', () => {
    cursor.classList.remove('hover');
  });
})

This works - but in addition I'd like to apply a different class when two links buttons are interacted with. The classes being .swiper-button-prev and .swiper-button-next. So probably something like hover-swiper-prev and hover-swiper-next depending on which one is interacted with.

Basically when I hover over an image carousel I'd like to display different cursor styling (arrows) for each link and rotate 180 degrees (so they point left/right) when switching to prev/next 'blocks'.

For context I'm using this great bit of code to create a custom javascript mouse cursor (it's for a portfolio/bit of fun so it's fine): https://codepen.io/ntenebruso/pen/QWLzVjY

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

0

Not sure about how you html looks, but here is a small snippet that might help. I added some css to demonstrate.

  1. Add the class you want to add as an attribute to the html elements.
  1. Grab that information to add and remove the class accordingly

const a = document.querySelectorAll("a");

const cursor = document.getElementById("cursor");

a.forEach((item) => {
  const interaction = item.dataset.interaction;

  item.addEventListener("mouseover", () => {
    cursor.classList.add(interaction);
  });
  item.addEventListener("mouseleave", (event) => {
    cursor.classList.remove(interaction);
  });
});
.swiper-button-prev{
    background-color:green;
}

.swipper-button-next{
     background-color:yellow;
}

.hover-swiper-prev{
     background-color:orange;
}

.hover-swiper-next{
    background-color:pink;
}
    <a href="" data-interaction="swiper-button-prev">Swipper prev</a>
    <a href="" data-interaction="swipper-button-next">Swipper next</a>
    <a href="" data-interaction="hover-swiper-prev">Hover prev</a>
    <a href="" data-interaction="hover-swiper-next">Hover next</a>

    <div style="width: 50px; height: 50px;" id="cursor">
      cursor
    </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!