Careless Careless - 7 months ago 20
Javascript Question

Show position where is clicked

Can someone tell me how to show position on mouse click. I am not asking for on click show div position... I am asking for this if I have :

<div id="div123" style="width: 300px; height: 300px;"></div>


And jquery :

$("#div123").click(function(){

var x = $("WHERE MOUSE CLICKED INSIDE #div123").position();

});

Answer

I have not tried, but this should work

$('#div123').click(function(e) {
    var posX = $(this).position().left
    var posY = $(this).position().top;
    alert( (e.pageX - posX) + ' , ' + (e.pageY - posY));
});
Comments