Using jQuery how to get click coordinates on the target element

JavascriptJqueryHtmlCssJquery Ui

Javascript Problem Overview


I have the following event handler for my html element

jQuery("#seek-bar").click(function(e){
    var x = e.pageX - e.target.offsetLeft;
    alert(x);    
});

I need to find the position of the mouse on the #seek-bar at the time of clicking. I would have thought the above code should work, but it gives incorrect result

Javascript Solutions


Solution 1 - Javascript

Are you trying to get the position of mouse pointer relative to element ( or ) simply the mouse pointer location

Try this Demo : http://jsfiddle.net/AMsK9/


Edit :

  1. event.pageX, event.pageY gives you the mouse position relative document !

Ref : http://api.jquery.com/event.pageX/
http://api.jquery.com/event.pageY/

  1. offset() : It gives the offset position of an element

Ref : http://api.jquery.com/offset/

  1. position() : It gives you the relative Position of an element i.e.,

consider an element is embedded inside another element

example :

<div id="imParent">
   <div id="imchild" />
</div>

Ref : http://api.jquery.com/position/

HTML

<body>
   <div id="A" style="left:100px;"> Default    <br /> mouse<br/>position </div>
   <div id="B" style="left:300px;"> offset()   <br /> mouse<br/>position </div>
   <div id="C" style="left:500px;"> position() <br /> mouse<br/>position </div>
</body>

JavaScript

$(document).ready(function (e) {

    $('#A').click(function (e) { //Default mouse Position 
        alert(e.pageX + ' , ' + e.pageY);
    });

    $('#B').click(function (e) { //Offset mouse Position
        var posX = $(this).offset().left,
            posY = $(this).offset().top;
        alert((e.pageX - posX) + ' , ' + (e.pageY - posY));
    });

    $('#C').click(function (e) { //Relative ( to its parent) mouse position 
        var posX = $(this).position().left,
            posY = $(this).position().top;
        alert((e.pageX - posX) + ' , ' + (e.pageY - posY));
    });
});

Solution 2 - Javascript

$('#something').click(function (e){
    var elm = $(this);
    var xPos = e.pageX - elm.offset().left;
    var yPos = e.pageY - elm.offset().top;

    console.log(xPos, yPos);
});

Solution 3 - Javascript

Try this:

jQuery(document).ready(function(){
   $("#special").click(function(e){
      $('#status2').html(e.pageX +', '+ e.pageY);
   }); 
})

Here you can find more info with DEMO

Solution 4 - Javascript

In percentage :

$('.your-class').click(function (e){
	var $this = $(this); // or use $(e.target) in some cases;
	var offset = $this.offset();
	var width = $this.width();
	var height = $this.height();
	var posX = offset.left;
	var	posY = offset.top;
	var x = e.pageX-posX;
		x = parseInt(x/width*100,10);
		x = x<0?0:x;
		x = x>100?100:x;
	var y = e.pageY-posY;
		y = parseInt(y/height*100,10);
		y = y<0?0:y;
		y = y>100?100:y;
	console.log(x+'% '+y+'%');
});

Solution 5 - Javascript

If MouseEvent.offsetX is supported by your browser (all major browsers actually support it), The jQuery Event object will contain this property.

> The MouseEvent.offsetX read-only property provides the offset in the X coordinate of the mouse pointer between that event and the padding edge of the target node.

$("#seek-bar").click(function(event) {
  var x = event.offsetX
  alert(x);    
});

Solution 6 - Javascript

see here enter link description here

html

<body>
<p>This is a paragraph.</p>
<div id="myPosition">
</div>
</body>

css

#myPosition{
  background-color:red;
  height:200px;
  width:200px;
}

jquery

$(document).ready(function(){
    $("#myPosition").click(function(e){
       var elm = $(this);
       var xPos = e.pageX - elm.offset().left;
       var yPos = e.pageY - elm.offset().top;
       alert("X position: " + xPos + ", Y position: " + yPos);
    });
});

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRomanView Question on Stackoverflow
Solution 1 - Javascriptuser372551View Answer on Stackoverflow
Solution 2 - JavascriptNisarView Answer on Stackoverflow
Solution 3 - JavascriptKrunalView Answer on Stackoverflow
Solution 4 - Javascriptl2aelbaView Answer on Stackoverflow
Solution 5 - JavascriptMemet OlsenView Answer on Stackoverflow
Solution 6 - JavascriptAirBlackView Answer on Stackoverflow