Skip to content Skip to sidebar Skip to footer

Event Listener Hover Changing Other Element

I made this script for showing/hiding other div that comes to place of the one with event (ricon1) on mouse in and out: HTML:
&

Solution 1:

Is there a specific reason you don't want to use jQuery for that?

Anyway, here's an example without jQuery:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>

</head>
<body>
    <div class = "switch">
        <div class = "icon">A</div>
        <div style = "display:none" class = "desc">Desc1</div>
    </div>
    <div class = "switch">
        <div class = "icon">B</div>
        <div style = "display:none" class = "desc">Desc2</div>
    </div>
    <div class = "switch">
        <div class = "icon">C</div>
        <div style = "display:none" class = "desc">Desc3</div>
    </div>
    <script>
        var icons = document.querySelectorAll('.switch');
        for (var i = 0; i < icons.length; i++) {
            icons[i].addEventListener("mouseenter", function() {
                (this.querySelectorAll(".icon")[0]).style.display = 'none';
                (this.querySelectorAll(".desc")[0]).style.display = 'block';
            });

            icons[i].addEventListener("mouseleave", function() {
                (this.querySelectorAll(".icon")[0]).style.display = 'block';
                (this.querySelectorAll(".desc")[0]).style.display = 'none';
            });
        }
    </script>
</body>
</html>

Post a Comment for "Event Listener Hover Changing Other Element"