1

I want to add class as I hover over an item and remove it as soon as the hover is removed in JavaScript. For now, my code is not removing the class until I hover over that item again.

    myChart.on('mouseover', function(params){
        var collapseItem = params.name + "-content"
        var toCollapse = document.getElementById(collapseItem)
        var collapseHeading = toCollapse.previousElementSibling
        collapseHeading.classList.toggle("active")
    })
3
  • Can you use to :hover css selector? Commented Jun 2, 2022 at 5:54
  • @TheKNVB I'm using a chart library and I want to highlight a specific text on my page as I hover the specific item in the graph. Commented Jun 2, 2022 at 5:59
  • 1
    You have to use mouseover as well as mouseout to make it work properly. myChart.on('mouseout', function(params){ var collapseItem = params.name + "-content" var toCollapse = document.getElementById(collapseItem) var collapseHeading = toCollapse.previousElementSibling collapseHeading.classList.toggle("active") // collapseHeading.classList.remove("active") }) Commented Jun 2, 2022 at 6:07

3 Answers 3

2

is this you want?

var myChart = document.getElementById("tt");
myChart.onmouseover = function(event){
        var collapseItem = event.target.tagName + "-content";

        var toCollapse = document.getElementById(collapseItem);
var collapseHeading = toCollapse.previousElementSibling;
        collapseHeading.classList.toggle("active");

}
#tt{
height: 40px;
width: 40px;
background-color: red;
}

.active{
height: 50px;
width: 50px;
background-color: green;
}
<div id="tt">
<div id="prev">
</div>

<div id="DIV-content">
</div>
</div>

Sign up to request clarification or add additional context in comments.

Comments

1

taken from this post

you can listen to mouseout event (taken from this post)

function toggleActiveClass(params){
    var collapseItem = params.name + "-content"
    var toCollapse = document.getElementById(collapseItem)
    var collapseHeading = toCollapse.previousElementSibling
    collapseHeading.classList.toggle("active")
}

myChart.on('mouseover', toggleActiveClass);
myChart.on('mouseout', toggleActiveClass);

3 Comments

Just need to remove the extra ')' from the end.
It worked but slight issue is that sometime mouseover isn't working on adjacent items in chart.
can you share an example?
1

You need to use "mouseover" as well as "mouseout" to make the toggle or class addition/removal work properly. Currently when your cursor leave there no function to remove the class.

function toggleActiveClass(params) {
    var collapseItem = params.name + "-content"    
    var toCollapse = document.getElementById(collapseItem)
    var collapseHeading = toCollapse.previousElementSibling
    collapseHeading.classList.toggle("active")
}

myChart.on('mouseover', toggleActiveClass);
myChart.on('mouseout', toggleActiveClass);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.