How to calculate mouse coordinates related to DIV position

javascript, jquery

Solution

Do you mean:

$('#someele').click(function(e) {
  var offset = $(this).offset();
  var x = Math.floor(e.pageX - offset.left);
  var y = Math.floor(e.pageY - offset.top);
  console.log('x pos:' +  x  + ' y pos:' + y);
});

Problem

I have a DIV and I can get the offset using .offset(). But I am trying to get the position of the mouse related to the div. When I hover the DIV i can get the x and y offsets of Mouse. But those will be calculated related to Document. But it should be calculated in below way. ``` For example DIV dimensions are 200 and 200. then it should calculate offsets related to (0,200)(200,0),(200,200),(200,200). ``` Please help me on this. How I can do this.

Original source