Calculating DIV Position in JavaScript

by Rekha 2009-11-07 15:23:40

The following code is used to calculate the top and left position for the div using javascript.

function getPosition(obj){
var topValue= 0,leftValue= 0;
while(obj){
leftValue+= obj.offsetLeft;
topValue+= obj.offsetTop;
obj= obj.offsetParent;
}
finalvalue = leftValue + "," + topValue;
return finalvalue;
}


You can use the getElementById to pass the element object.

var d = document.getElementById('divname');
getPosition(d);

Tagged in:

1355
like
0
dislike
0
mail
flag

You must LOGIN to add comments