Image Click Coordinates with jQuery

Click an image to calculate the pointer position inside the image. Using getBoundingClientRect() with clientX/clientY works reliably even when the page is scrolled.

Image used to demonstrate x and y click coordinates
Click the image.

Calculate Image-Relative Coordinates Top ↑

$( "#coordinate-image" ).on(
    "click",
    function ( event ) {
        const rect =
            this.getBoundingClientRect();

        const x = Math.round(
            event.clientX - rect.left
        );

        const y = Math.round(
            event.clientY - rect.top
        );

        $( "#coordinate-result" ).text(
            "X: " + x + ", Y: " + y
        );
    }
);

The old pageX - offsetLeft pattern can be wrong when offset parents or scrolling are involved.

Original image-coordinate demo →

← jQuery Events · Pointer coordinates

Original Source Examples Retained for Migration Reference Top ↑

These source-page examples are preserved so no learner-purpose code is silently lost. Use the modern examples above as the recommended implementation.

Original example 1

<script>
$(document).ready(function() {
$("img").on("click", function(event) {
var x = event.pageX - this.offsetLeft;
var y = event.pageY - this.offsetTop;
$('#d1').html("X Coordinate: " + x + ", Y Coordinate: " + y )
});
});
</script>

Additional Original Learning Routes Top ↑






plus2net.com






✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer