2

I am aware of the fact that variations of that question were asked many times before. Looking through Google output I found pages with examples like that:

function point_it(event) {
pos_x = event.offsetX ? (event.offsetX) : event.pageX - document.getElementById("pointer_div").offsetLeft;
pos_y = event.offsetY ? (event.offsetY) : event.pageY - document.getElementById("pointer_div").offsetTop;
document.pointform.form_x.value = pos_x;
document.pointform.form_y.value = pos_y;
}

The code works, but when I look at MouseEvent.offsetX it says "This is an experimental technology...".

So my question is, is it save to use the above construction or not? Is it better to learn how to use JQuery?

6
  • jQuery IS javascript - therefore if you want to do this in plain vanilla JS, it CAN be done Commented Jan 7, 2016 at 9:26
  • It is better to learn javascript. Once you understand javascript you will know if you should use jquery or not. Commented Jan 7, 2016 at 9:28
  • If IE 6 supports this, the other browsers must support it. Commented Jan 7, 2016 at 9:28
  • @Darren Sweeney what do you mean by "plain vanilla" JS? Isn't my example written in JS? Commented Jan 7, 2016 at 10:15
  • Yes, but your question asked whether you could do it in Javascript or better to learn jQuery - a common term for JS without jQuery is vanilla JS Commented Jan 7, 2016 at 10:17

1 Answer 1

3

Please try this

function getClickPosition(e) {
    var xPosition = e.clientX;
    var yPosition = e.clientY;
}

Call this function on "onClick" event. You can get the coordinates. Output the coordinates of the mouse pointer when the mouse button is clicked on an element.

Sign up to request clarification or add additional context in comments.

1 Comment

That function is pretty simple and works fine. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.