Javascript Button Hover works only once

I'm trying to implement some button which displays list items on hover.

It works good however it stops working after the first hover then I need to keep refreshing the page for it to work again.

Please see the code below;

var btn = document.getElementsByClassName("collapsible"); 
            
                    btn[0].addEventListener("click", function () { 
                        this.classList.toggle("active"); 
                        var content = this.nextElementSibling; 
                        if (content.style.display === "block") { 
                            content.style.display = "none"; 
                        } else { 
                            content.style.display = "block"; 
                        } 
                    }); 

Is there anyway I can make it work again and again without having to refresh the page?



Read more here: https://stackoverflow.com/questions/66269846/javascript-button-hover-works-only-once

Content Attribution

This content was originally published by Min Kyung Kwon at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: