Using Jquery How to Get Click Coordinates on the Target Element

Using jQuery how to get click coordinates on the target element

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/

2) offset() : It gives the offset position of an element

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

3) 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));
});
});

Using jQuery how to get right click coordinates on the target element

You can try:

$('div').on('contextmenu', function (e) {
console.log(e.pageX);
console.log(e.pageY);
});

And for whole page:

$('div').on('contextmenu', function (e) {
console.log(e.clientX);
console.log(e.clientY);
});

Fiddle: https://jsfiddle.net/shree/awf5u2xx/1/

getting the X/Y coordinates of a mouse click on an image with jQuery

You can use pageX and pageY to get the position of the mouse in the window. You can also use jQuery's offset to get the position of an element.

So, it should be pageX - offset.left for how far from the left of the image and pageY - offset.top for how far from the top of the image.

Here is an example:

$(document).ready(function() {
$('img').click(function(e) {
var offset = $(this).offset();
alert(e.pageX - offset.left);
alert(e.pageY - offset.top);
});
});

I've made a live example here and here is the source.

To calculate how far from the bottom or right, you would have to use jQuery's width and height methods.

How do I get the coordinates of a mouse click on a canvas element?

If you like simplicity but still want cross-browser functionality I found this solution worked best for me. This is a simplification of @Aldekein´s solution but without jQuery.

function getCursorPosition(canvas, event) {
const rect = canvas.getBoundingClientRect()
const x = event.clientX - rect.left
const y = event.clientY - rect.top
console.log("x: " + x + " y: " + y)
}

const canvas = document.querySelector('canvas')
canvas.addEventListener('mousedown', function(e) {
getCursorPosition(canvas, e)
})

How to simulate a click by using x,y coordinates in JavaScript?

You can dispatch a click event, though this is not the same as a real click. For instance, it can't be used to trick a cross-domain iframe document into thinking it was clicked.

All modern browsers support document.elementFromPoint and HTMLElement.prototype.click(), since at least IE 6, Firefox 5, any version of Chrome and probably any version of Safari you're likely to care about. It will even follow links and submit forms:

document.elementFromPoint(x, y).click();
  • https://developer.mozilla.org/en-US/docs/Web/API/Document/elementFromPoint
  • https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/click

Can I get the exact click location relative to the element being clicked?

The event which is passed to the jQuery handler contains the co-ordinates of the click event relevant to the parent DOMElement within the offsetX and offsetY properties. Try this:

$('#foo').click(function (e){
console.log(e.offsetX, e.offsetY);
});

Example fiddle

jQuery get mouse position within an element

One way is to use the jQuery offset method to translate the event.pageX and event.pageY coordinates from the event into a mouse position relative to the parent. Here's an example for future reference:

$("#something").click(function(e){
var parentOffset = $(this).parent().offset();
//or $(this).offset(); if you really just want the current element's offset
var relX = e.pageX - parentOffset.left;
var relY = e.pageY - parentOffset.top;
});

jQuery onclick: position X / Y

To get the coords relatively the window use pageX and pageY.

The mouse position relative to the left edge of the document

Also, you can calculate relatively the element by increase the .relative position, like this:

$(".relative").click(function(e) {  var offset = $(this).offset();  $(".clickIndicator").css({    "left": e.pageX - offset.left + "px",    "top": e.pageY - offset.top + "px",    "display": "block"  });});
.relative {  position: relative;  width: 500px;  height: 300px;  background: black;}.absolute {  position: absolute;  height: 30px;  width: 30px;  background: white;}.clickIndicator {  position: absolute;  border-radius: 100px;  width: 10px;  height: 10px;  background: red;  display: none;  z-index: 1;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><div class="relative">  <div class="clickIndicator"></div>  <div class="absolute" style="top: 30px; left: 90px;"></div>  <div class="absolute" style="top: 200px; left: 340px;"></div>  <div class="absolute" style="top: 140px; left: 79px;"></div>  <div class="absolute" style="top: 230px; left: 211px;"></div>  <div class="absolute" style="top: 90px; left: 450px;"></div>  <div class="absolute" style="top: 80px; left: 260px;"></div></div>

Set up a click event using mouse position in JQuery

$(document).click(function(event) {
var top = 0,
right = 200,
bottom = 200,
left = 0;

var x = event.pageX;
var y = event.pageY;
if ((x >= left && x <= right) && (y >= top && y <= bottom))
{
// do stuff if within the rectangle
}
});


Related Topics



Leave a reply



Submit