Custom Search

Calculating DIV Position in JavaScript

The following code snippet will return you the top and left position for DIV element used in your web page.

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





Arguments:
Need to pass the Element Object.

Example:

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

NOTE:
This code can also be used to calculate HTML element position also.

Your email:  
Subscribe Unsubscribe  

Similar Posts

Did you enjoy this post? Why not leave a comment below and continue the conversation, or subscribe to my feed and get articles like this delivered automatically to your feed reader.

Comments

good example.
i was findint it since long time.
thank u

How to calculate this if my div has display property absolute or relative?

Leave a comment

(required)

(required)