While you have two answers already, I thought I'd post an alternative to those; using one named function instead of two anonymous functions:
// defining a named function, using arrow syntax:
let toggleClassOnHover = (e) => {
// here we retrieve the element with the id of 'elementTwo':
document.getElementById('elementTwo')
// we use the classList API to work with the classes
// of that element:
.classList
// using the toggle method to add, or remove, the
// 'hoverClass' class-name to the element, depending
// on the switch:
.toggle('hoverClass',
// if the 'type' property of the Event object ('e')
// is of the same type and value as 'mouseenter'
// the switch is (Boolean) true and the class-name is added
// (if it is not already present); if it is not exactly
// equal to 'mouseenter' then the switch is false and so the
// class is removed (if it is already present):
e.type === 'mouseenter');
},
// we're adding two event-listeners to this element so we cache
// a reference to the element:
elementOne = document.getElementById('elementOne');
// here we add an event-listener for the 'mouseenter' event,
// binding the named function as the event-handler:
elementOne.addEventListener('mouseenter', toggleClassOnHover);
// here we add an event-listener for the 'mouseleave' event,
// binding the named function as the event-handler:
elementOne.addEventListener('mouseleave', toggleClassOnHover);
let toggleClassOnHover = (e) => {
document.getElementById('elementTwo').classList.toggle('hoverClass', e.type === 'mouseenter');
},
elementOne = document.getElementById('elementOne');
elementOne.addEventListener('mouseenter', toggleClassOnHover);
elementOne.addEventListener('mouseleave', toggleClassOnHover);
div {
width: 80vw;
margin: 0.5em auto;
border: 2px solid currentColor;
transition: color 0.4s linear;
}
.hoverClass {
color: limegreen;
}
<div id="elementOne">Element one</div>
<div id="elementTwo">Element two</div>
Why did I use the mouseenter
and mouseleave
events, instead of mouseover
and mouseout
? That's illustrated in the following demo:
function incrementCounts(e) {
if (event.type === 'mouseover') {
+this.dataset.mouseovercount++;
} else if (event.type === 'mouseenter') {
+this.dataset.mouseentercount++;
}
}
let elementOne = document.getElementById('elementOne');
elementOne.addEventListener('mouseover', incrementCounts);
elementOne.addEventListener('mouseenter', incrementCounts);
div {
width: 80vw;
margin: 0.5em auto;
border: 2px solid currentColor;
transition: color 0.4s linear;
}
div p {
width: 50vw;
margin: 0.5em auto;
border: 2px solid #000;
}
#elementOne::before,
#elementOne::after {
display: inline-block;
width: 100%;
border: 1px solid transparent;
height: 1em;
}
#elementOne::before {
content: 'Mouseover count: ' attr(data-mouseovercount);
border-bottom-color: #000;
}
#elementOne::after {
content: 'Mouseenter count: ' attr(data-mouseentercount);
border-top-color: #000;
}
.hoverClass {
color: limegreen;
}
<div id="elementOne" data-mouseovercount="" data-mouseentercount="">
<p>Element one's content wrapped in a strangely sized <p> element, itself with a <span><span> descendent.</p></div>
<div id="elementTwo">Element two</div>
At its most basic the mouseover
event fires when the cursor enters the bounds of the element itself and when entering the bounds of the descendent elements; whereas mouseenter
fires only when the cursor crosses into the element itself.
In this particular case it makes little difference but, in other situations, mouseover
can lead to unexpected outcomes, whereas mouseenter
is somewhat more predictable.
As you posted no HTML of your own we're unable to offer specifics as to your particular problem, but if the second element appears in the DOM as a later sibling, or descendent of a later sibling, of the first element then CSS is quite able to offer this functionality if styling is your only requirement:
div {
width: 80vw;
margin: 0.5em auto;
border: 2px solid currentColor;
transition: color 0.4s linear;
}
#elementOne:hover+#elementTwo {
color: limegreen;
}
<div id="elementOne">Element one</div>
<div id="elementTwo">Element two</div>
References: