Your Ad Here

Getting X And Y Axis Co-ordinates (Javascript)

DEMO :


Source Code :
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function GetScreenCordinates(obj) {
var p = {};
p.x = obj.offsetLeft;
p.y = obj.offsetTop;
while (obj.offsetParent) {
p.x = p.x + obj.offsetParent.offsetLeft;
p.y = p.y + obj.offsetParent.offsetTop;
if (obj == document.getElementsByTagName("body")[0]) {
break;
}
else {
obj = obj.offsetParent;
}
}
return p;
}
</script>
<script type = "text/javascript">
function GetTextboxCordinates() {
var txt = document.getElementById("txtText");
var p = GetScreenCordinates(txt);
alert("X:" + p.x + " Y:" + p.y);
}
</script>
</head>
<body>
<form id="form1">
<input id = "txtText" type = "text" /><br/><br/>
<input value = "Get Coordinates" type = "button" onclick = "GetTextboxCordinates()" />
</form>
</body>
</html>


The above code has been tested in the following browsers
Internet Explorer FireFox Chrome Safari Opera

* All browser logos displayed above are property of their respective owners.

0 comments:

Post a Comment