console
var elmids = ['divid'];
var x, y = 0;
function getXYpos(elm) {
x = elm.offsetLeft;
y = elm.offsetTop;
elm = elm.offsetParent;
while (elm != null) {
x = parseInt(x) + parseInt(elm.offsetLeft);
y = parseInt(y) + parseInt(elm.offsetTop);
elm = elm.offsetParent;
}
return {
'xp': x,
'yp': y
};
}
function getCoords(e) {
var xy_pos = getXYpos(this);
if (navigator.appVersion.indexOf("MSIE") != -1) {
var standardBody = (document.compatMode == 'CSS1Compat') ? document.documentElement: document.body;
x = event.clientX + standardBody.scrollLeft;
y = event.clientY + standardBody.scrollTop;
} else {
x = e.pageX;
y = e.pageY;
}
x = x - xy_pos['xp'];
y = y - xy_pos['yp'];
document.getElementById('coords').innerHTML = 'X= ' + x + ' ,Y= ' + y;
}
for (var i = 0; i < elmids.length; i++) {
if (document.getElementsByClassName(elmids[i])) {
let divGot = document.getElementsByClassName(elmids[i]);
for (var a = 0; a < divGot.length; a++) {
divGot[a].onmousemove = getCoords;
}
document.getElementById(elmids[i]).onclick = function() {
document.getElementById('regcoords').value = x + ' , ' + y;
};
}
}
<div class="divid">
</div>
<div class="divid">
</div>
<div>
Click to add the coordinates in this text field.
</div>
<input type="text" name="regcoords" id="regcoords" />
<div id="coords">
Coords
</div>
body {
background: coral;
}
div {
margin: 50px;
color: white;
}
.divid {
display: inline-block;
height: 250px;
width: 100px;
background: #0809fe;
}