How to Open a Popup on Hover using JavaScript ?
Last Updated :
18 Mar, 2024
In JavaScript, to perform the functionality of opening the popup on hover, we can dynamically manipulate the DOM by responding to the mouse events.
The below approaches can be used to accomplish this task.
Using mouseover and mouseout events
In this approach, we are using the JavaScript events mouseover and mouseout to toggle the display of the popup content. The mouseover event is used to display the popup while mouseout is used to hide the same.
Syntax:
element.addEventListener(event, listener, useCapture);
Example: The below example uses JavaScript Events to open and hide a popup on hover using JavaScript.
HTML
<!DOCTYPE html>
<html>
<head>
<title>Example 1</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
padding: 50px;
}
.popup-container {
color: green;
display: inline-block;
position: relative;
}
.popup-content {
display: none;
position: absolute;
top: 100%;
left: 50%;
transform: translateX(-50%);
padding: 10px;
background-color: #f1f1f1;
border: 1px solid #ccc;
border-radius: 5px;
}
</style>
</head>
<body>
<h3>
Using mouseover and mouseout JavaScript Events
</h3>
<div class="popup-container" id="popupContainer">
Hover me to see popup
<div class="popup-content" id="popupContent">
Hey Geek, <br />
Welcome to GeeksforGeeks!!
</div>
</div>
<script>
const popupContainer =
document.getElementById('popupContainer');
const popupContent =
document.getElementById('popupContent');
popupContainer.addEventListener
('mouseover', function () {
popupContent.style.display = 'block';
});
popupContainer.addEventListener
('mouseout', function () {
popupContent.style.display = 'none';
});
</script>
</body>
</html>
Output:

Using onmouseenter and onmouseleave events
In this approach, we are using the onmouseenter and onmouseleave events directly in JavaScript to control the display of a popup on hover. The script sets the display property of the popup content to 'block' on mouse enter and 'none' on mouse leave.
Syntax:
object.onmouseenter = function(){myScript};
object.onmouseleave = function(){myScript};
Example: The below example uses onmouseenter and onmouseleave events to open a popup on hover using JavaScript.
HTML
<!DOCTYPE html>
<head>
<title>Example 2</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
padding: 50px;
}
.popup-container {
display: inline-block;
position: relative;
}
.popup-content {
display: none;
position: absolute;
top: 120%;
left: 50%;
transform: translateX(-50%);
padding: 15px;
background-color: #ebdb00;
color: green;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
opacity: 0;
transition: opacity 0.3s ease-in-out,
top 0.3s ease-in-out;
}
.popup-container:hover
.popup-content {
top: 100%;
opacity: 1;
}
</style>
</head>
<body>
<h3>
Using onmouseenter and
onmouseleave Attributes
</h3>
<div class="popup-container" id="popupContainer">
Hover me to see popup
<div class="popup-content">
Hey Geek,
<strong>
Welcome to GeeksforGeeks!
</strong>
</div>
</div>
<script>
const popupContainer =
document.getElementById('popupContainer');
const popupContent =
document.querySelector('.popup-content');
popupContainer.onmouseenter =
function () {
popupContent.style.display = 'block';
};
popupContainer.onmouseleave =
function () {
popupContent.style.display = 'none';
};
</script>
</body>
</html>
Output:

Similar Reads
How to Open a Popup on Click using JavaScript ? Opening a Pop-up when the click action is performed is the user-interactive technique. We can use this functionality in messages, or forms to show some error or alert messages before the user moves to the next action.Table of ContentUsing display propertyUsing classList PropertyUsing visibility prop
4 min read
How to Toggle Popup using JavaScript ? A popup is a graphical user interface element that appears on top of the current page, often used for notifications, alerts, or additional information. These are the following approaches to toggle a popup using JavaScript: Table of Content Using visibility propertyUsing ClassListUsing visibility pro
3 min read
How to Open URL in New Tab using JavaScript? To open a URL in a new tab using JavaScript, we can use the window.open() method. The window.open() method is used to open a new browser window or tab. It can also be used to load a specific URL in a new tab. Syntaxwindow.open(URL, '_blank');window.open(): Opens a new tab or window.First Parameter:
3 min read
How to create Popup Box using HTML CSS and JavaScript? Creating a popup box with HTML, CSS, and JavaScript improves user interaction on a website. A responsive popup appears when a button is clicked, featuring an HTML structure, CSS for styling, and JavaScript functions to manage visibility.ApproachCreate the Popup structure using HTML tags, Some tags a
3 min read
How to open URL in a new window using JavaScript? In HTML, the anchor tag (<a>) is used to open new windows and tabs in a very straightforward manner. However, there are situations where you need to achieve the same functionality using JavaScript. This is where the window.open() method becomes useful.The window.open() method is used to open a
3 min read