JavaScript onmouseover and onmouseout Example
Learn how JavaScript onmouseover and onmouseout events fire when the pointer enters or leaves an element, then use them to build a simple color-changing hover effect.


In JavaScript, the “onmouseover” and “onmouseout” event listeners are events that fire when the mouse cursor enters or leaves an HTML element. These event listeners can be attached to HTML elements as an “event handler”, and they run the JavaScript code that defines a behavior for those elements.
The “onmouseover” event listener fires when the mouse cursor enters an HTML element. This event is generally used for purposes such as showing a description when the cursor enters an element, or making a visual change.
The “onmouseout” event listener fires when the mouse cursor leaves an HTML element. This event is generally used for purposes such as hiding a description when the cursor leaves an element, or reverting a visual change.
As an example, the following HTML code adds “onmouseover” and “onmouseout” event listeners to a div element and sets a different background color when those events fire:
<div onmouseover="this.style.backgroundColor = 'yellow'" onmouseout="this.style.backgroundColor = 'white'">
Fare imleci bu div elementinin üzerine geldiğinde arka plan rengi değişecektir.
</div>
This code makes the background color “yellow” when the mouse cursor enters this div element, and changes the background color to “white” when the mouse cursor leaves the element.
Note: Nowadays, instead of the “onmouseover” and “onmouseout” event listeners, the “addEventListener()” method is generally used, and event listeners are added dynamically with this method. This approach is preferred because it is more flexible and scalable than the “onmouseover” and “onmouseout” event listeners.
We’ll use the addEventListener we’re talking about here a lot later on, so we can go into a bit more detail without splitting up this article.
We use “addEventListener()” to add an event listener to an HTML element. This method specifies an event type and the code to run when the event fires. This way, event listeners can be added dynamically to define a behavior for an element, and the behavior of those elements can be changed at run-time (while the code is running).
The usage of the “addEventListener()” method is as follows:
element.addEventListener(eventType, functionToCall);
In this method, the “element” parameter represents an HTML element, and the “eventType” parameter specifies which type the added event listener belongs to. For example, the event types can be “click”, “mouseover”, “keydown”, and so on. The “functionToCall” parameter, on the other hand, represents the code to run when the event listener fires. This code can be a function expression or a function declaration.
As an example, the following code adds an event listener that listens for the “mouseover” event to a div element and makes the background color “yellow” when that event fires:
const divElement = document.querySelector('div');
divElement.addEventListener('mouseover', function() {
this.style.backgroundColor = 'yellow';
});
This code adds an event listener that listens for the “mouseover” event and makes the background color “yellow” when that event fires. This event listener was added dynamically using the “addEventListener()” method and can be changed at run-time (while the code is running).
Note: The “addEventListener()” method can listen for many different event types and can dynamically determine the code to run when those events fire. Therefore, the “addEventListener()” method is a more flexible and scalable approach than old-style event listeners such as “onmouseover” and “onmouseout”.
Share it if it helped


