The Wayback Machine - https://web.archive.org/web/20241116153115/https://www.geeksforgeeks.org/how-to-determine-which-element-the-mouse-pointer-move-over-using-javascript/
Open In App

How to Determine which Element the Mouse Pointer Move Over using JavaScript?

Last Updated : 09 Oct, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Given an HTML document and the task is to get the element where the mouse pointer moves over.

Below are the approaches to Determining which Element the Mouse Pointer moves over using JavaScript:

Approach 1: Using clientX and clientY properties

  • Get the x and y coordinates value by using .clientX and .clientY properties.
  • Use document.elementFromPoint(x, y) method to get the element content on that position when the mouse pointer moves over.

Example 1: This example implements the above approach.

html
<!DOCTYPE HTML>
<html>

<head>
    <title>
        How to determine which element the mouse
        pointer move over using JavaScript ?
    </title>
</head>

<body style="text-align:center;">

    <h1 style="color:green;" onmouseover="GFG_Fun()">
        GeeksForGeeks
    </h1>

    <p id="GFG_UP" onmouseover="GFG_Fun()"
        style="font-size: 15px; font-weight: bold;">
    </p>

    <button onmouseover="GFG_Fun()">
        click here
    </button>

    <p id="GFG_DOWN" style="font-size: 24px; font-weight: bold; color: green;">
    </p>

    <script>
        let up = document.getElementById('GFG_UP');
        let down = document.getElementById('GFG_DOWN');
        up.innerHTML = "Hover over the document to know the element.";

        function GFG_Fun() {
            let x = event.clientX;
            let y = event.clientY;
            el = document.elementFromPoint(x, y);
            down.innerHTML = el.innerHTML;
        } 
    </script>
</body>

</html>

Output:

output

Approach 2: Using onmouseover property

  • Attach the event ‘onmouseover‘ to the element.
  • Call the alert function with the id of that element, each time when event occurs.

Example: This example using the approach discussed above.

html
<!DOCTYPE HTML>
<html>

<head>
    <title>
        How to determine which element the mouse
        pointer move over using JavaScript ?
    </title>
</head>

<body style="text-align:center;">

    <h1 id="h1" style="color:green;" onmouseover="alert(this.id)">
        GeeksForGeeks
    </h1>

    <p id="p" onmouseover="alert(this.id)"
        style="font-size: 15px; font-weight: bold;">
        Hover over the document to know the element.
    </p>

    <button id="button" onmouseover="alert(this.id)">
        click here
    </button>
</body>

</html>

Output:

output


Similar Reads

How to move mouse pointer to a specific position using JavaScript ?
In this article, we will learn how to move a mouse pointer to any specific position in the web browser using JavaScript. Before we start, you need to know that it's not actually possible to move the mouse pointer to a position using Javascript, such functionality can be easily misused but we can simulate something similar. In this article, we will
4 min read
How to Change Background Color of a Div on Mouse Move Over using JavaScript ?
The background color of the div box can be easily changed using HTML, CSS, and Javascript. We will use the querySelector() and addEventListener() methods to select the element and then apply some math logic to change its background color. The below sections will guide you on how to create the effect. HTML Code: In this section, we will create a bas
2 min read
How to split text horizontally on mouse move over using CSS ?
The SPLIT or BREAK effect is a captivating technique where text splits horizontally on hover, enhancing user interaction. Using CSS transitions, pseudo-elements, and positioning, this effect creates dynamic, layered text with smooth transitions, resulting in an engaging and visually striking animation. ApproachHTML Structure: Use an <h1> elem
2 min read
Create an object that follows the mouse pointer using p5.js
In this article, we are going to create an object, that will follow the mouse pointer. As the mouse circle move, that entity will follow the mouse arrow with some smooth speed, we can achieve this effect y using p5.js. It is a powerful JavaScript library for creating interactive graphics and animations in a web browser. Approach: Create a canvas fo
3 min read
How to get the Position of mouse pointer in jQuery ?
In this article, we will see how to get the position of the mouse pointer using jQuery. To get the position of mouse pointer, event.pageX, and event.pageY property is used. The event.pageX property is used to find the position of the mouse pointer relative to the left edge of the document. The event.pageY property is used to find the position of th
1 min read
How to create a pop-up div on mouse over and stay when click using jQuery ?
In this article, we will learn how to create a pop-up div on mouseover and stay when click using jQuery. Approach: First, we create an HTML div element that we want to pop up when we mouse over on an element and set its display property to none in CSS style.display:none;In the script tag, we create a variable flag and set its value to -1. $flag = -
2 min read
How to Change the Cursor into a Hand Pointer on Hover over list using CSS
CSS offers a powerful property called "cursor" that controls the appearance of the mouse pointer when hovering over specific HTML elements. By assigning different values to this property, you can customize the cursor's behaviour. In our case, we'll be using the value "pointer" to trigger the classic hand icons. This slight visual cue plays a signif
2 min read
Bottom Half Hidden Text Revealer on Mouse Over in CSS
In this CSS effect, the bottom half of the text is hidden, and when the user hovers over the text, some portion of the text becomes visible. This effect can be created by giving 0 brightness to text to be hidden and using the clip-path property to make it visible. JavaScript is used to get the cursor position. Approach: Create an HTML file with the
2 min read
How To Move Branch Pointer To Different Commit Without Checkout?
Git pros frequently utilize this strong approach to rebase branches, correct errors, and modify branch histories: shifting the branch pointer to a different commit without checking out. With this approach, you can modify the commit that a branch points to without affecting the status of your working directory. When you need to make corrections to t
1 min read
jQuery | Move an element into another element
There are two methods to move an element inside another element are listed below: Method 1: Using append() method: This append() Method in jQuery is used to insert some content at the end of the selected elements. Syntax: $(selector).append( content, function(index, html) ) Parameters: This method accepts two parameters as mentioned above and descr
2 min read
How to add border to an element on mouse hover using CSS ?
Adding a border to an element on mouse hover using CSS involves changing the element's border style when the mouse pointer is over it. This effect enhances user interaction by visually highlighting the element, achieved using the :hover pseudo-class to modify border properties. Approach HTML Structure: Select the HTML element you want to apply the
1 min read
How to Style Parent Element when Hover Over a Child Element in CSS ?
To Style Parent Element When Hovering Over A Child Element is a technique of changing how a parent looks when you hover over a child element can make your designs more engaging and interesting and enhance user interaction on your website or app. Syntax:child-element { pointer-events: auto } child-element:hover { /* Styles for child element */ } par
2 min read
How to move an array element from one array position to another in JavaScript?
In JavaScript, we can access an array element as in other programming languages like C, C++, Java, etc. Also, there is a method called splice() in JavaScript, by which an array can be removed or replaced by another element for an index. So to move an array element from one array position to another we can splice() method or we can simply use array
5 min read
How to Configure Mouse Wheel Speed Across Browsers using JavaScript ?
The mouse wheel's scrolling speed varies with the choice of the web browser, even the DOM events and methods to change the scrolling speed are not the same. To provide zoom and animation on a web page, it is generally required to configure mouse speed. The speed of the wheel can be controlled by normalizing the distance the wheel has traveled. Ther
3 min read
How to differentiate mouse “click” and “drag” event using JavaScript ?
Working with web elements a user may drag or click an element as per requirement. It is important to distinguish between drag and click events. JavaScript is a high-level, dynamically typed programming language that can be used to distinguish between drag and click events. JavaScript has drag-and-clicking events that help to differentiate between t
3 min read
Design Hit the Mouse Game using HTML, CSS and Vanilla Javascript
In this article, we are going to create a game in which a mouse comes out from the holes, and we hit the mouse with a hammer to gain points. It is designed by using HTML, CSS & Vanilla JavaScript. HTML Code: First, we create an HTML file (index.html).Now after creating the HTML file, we are going to give a title to our webpage using the <tit
5 min read
How to ignore mouse interaction on overlay image using JavaScript ?
Ignoring mouse interaction on overlay images can be controlled by a CSS property namely pointer-events. It is a property that defines if an element of the page will react to the pointer-events. Syntax: pointer-events: auto\none;Example: The following example demonstrates the use of pointer-events property. [GFGTABS] html <!DOCTYPE html> <h
1 min read
How to Play Video on Mouse Hover and Pause on Mouseout using JavaScript ?
In this article, we learn how to play a video when the mouse hovers on it and how to stop it when the mouse is removed from the video element. Sometimes there is a need where we want that video on websites to automatically starts playing when the user keeps the mouse over it because it reduces the number of clicks by the user on the website and giv
2 min read
How to Change Background Color According to Mouse Location using JavaScript?
To change the background color based on the mouse location in JavaScript, we can track the cursor's position on the screen and adjust the background color as the mouse moves. This is done by capturing the X and Y coordinates of the mouse and applying those values to change the background color dynamically. HTML: We start with a simple HTML structur
1 min read
How to get the coordinates of a mouse click on a canvas element ?
The coordinates of the mouse whenever a click takes place can be found by detecting the click event with an event listener and finding the event's x and y positions. In this article we are going to learn How to get the coordinates of a mouse click on a canvas element. ApproachCreate a JavaScript function getMousePosition that takes the canvas eleme
2 min read
How to move an element to left, right, up and down using arrow keys ?
The task is to move an element to left, right, up and down using arrow keys in jquery, we can use the jQuery keydown() method along with the .animate() method. The keydown() method triggers the keydown event whenever User presses a key on the keyboard. Syntax: $(selector).keydown(function) Approach: The .keydown() method is to used check whether an
3 min read
How to Move an Element in a Circular Path using CSS ?
In this article, we will see how to move an element in a circular path using CSS. Approach: We will create an HTML element, and add some CSS styles to it. We will use the animation property to add an animation effect to move the element in a circular path.Add some CSS styles to set the width, height, border-radius, and background-color of the box i
2 min read
How to move one DIV element inside another using jQuery ?
In this article, we will learn to move one HTML div element inside another using jQuery. The HTML div element is used to define a division or a section in an HTML document. Method used: parent(): This method is used to get the parent of each element in the current set of matched elements, optionally filtered by a selector.detach(): This method is u
2 min read
How to Skip Over an Element in .map() in JavaScript?
The .map() method in JavaScript is used for transforming arrays by applying a function to each element. In some cases, we need to skip some elements during the mapping process. The .map() method always returns an array of the same length as the original. This article cover various approaches to skip elements in .map(). Returns undefined to Skip Ele
2 min read
How to Synchronously determine a JavaScript Promise's State
Handling asynchronous operations in JavaScript can be challenging. However, Promises has made this task easier by providing a more elegant and better error management solution. Although Promise-based code is primarily designed to work asynchronously, there may be instances where you need to synchronously determine a Promise's state for various reas
3 min read
JavaScript Program to Determine the Unicode Code Point at a Given Index
It is important to know what the Unicode code points are before moving on to the JavaScript implementation. A Unicode code point is a distinct integer value that represents each character in a string. A programmer may easily manipulate strings using JavaScript's techniques for working with these code points. Table of Content Using the codePointAt()
3 min read
How to compare two objects to determine the first object contains equivalent property values to the second object in JavaScript ?
In this article, we are going to learn about comparing two objects to determine if the first object contains equivalent property values to the second object, In JavaScript, comparing the values of two objects involves checking if they have the same properties with corresponding values. Given two objects obj1 and obj2 and the task are to check that
6 min read
JavaScript Program to Determine the Frequency of Elements in an Array and Represent it as an Object
In this article, we are going to learn how can we find the frequency of elements in an array and represent it as an object. The frequency of elements in an array in JavaScript refers to how many times each unique element appears in the array. These are the following approaches by using these we can find the frequency of elements: Table of Content U
4 min read
Which built-in method removes the last element from an array and returns that element in JavaScript ?
In this article, we will know how to remove the last element from an array using the built-in method in Javascript, along with understanding its implementation through the examples. There are a few Javascript built-in methods and techniques with which we can remove the last element of the array. We will focus only on the 2 methods ie., the pop() me
3 min read
Delete Without Head Pointer using JavaScript
The objective is to remove a node in a JavaScript singly linked list without having a direct reference to the head node. This entails making changes to the node that will be removed without actually using the head pointer. Below are the approaches to deleting a node without a head pointer using JavaScript: Table of Content Copy Data and Adjust Poin
6 min read
Article Tags :
three90RightbarBannerImg