Open In App

HTML DOM MouseEvent clientX Property

Last Updated : 01 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The mouse event clientX property returns the horizontal coordinate (X-axis) of the mouse pointer in relation to the viewport when a mouse event occurs. It captures the mouse’s position without considering any scroll offset.

Syntax:

event.clientX

Return Value: clientX returns the mouse pointer’s X-coordinate relative to the viewport.

Example: The below example Finds the X-coordinates of the mouse pointer when the mouse clicked anywhere in viewport.

html
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>MouseEvent.clientX Example</title>
</head>

<body>
    <h3>Click anywhere to get the mouse X position</h3>
    <p id="output">Mouse X position will appear here.</p>

    <script>
        document.addEventListener('click', function (event) {
            const mouseX = event.clientX;
            document.getElementById('output').textContent = `Mouse X: ${mouseX}px`;
        });
    </script>
</body>

</html>

Output:

mouseEvent

Mouse Event

Supported Web Browsers:

  • Opera
  • Google Chrome
  • Firefox
  • Apple Safari

Next Article
Article Tags :

Similar Reads