Need to find the absolute position of an HTML element

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AdrianH
    Recognized Expert Top Contributor
    • Feb 2007
    • 1251

    Need to find the absolute position of an HTML element

    All HTML elements have a bounding rect. I want to know what that bounding rect is relitive to the document window. Does anybody know how to find this using JavaScript?


    Adrian
  • mrhoo
    Contributor
    • Jun 2006
    • 428

    #2
    Code:
    function locate(hoo){	
    	var T=0,L=0;
    	while(hoo){
    		L+= hoo.offsetLeft || 0;
    		T+= hoo.offsetTop || 0;
    		if(hoo== document.body) break;
    		hoo= hoo.parentNode;		
    	}
    	var A= [L,T];
    	return  A;
    }

    Comment

    • AdrianH
      Recognized Expert Top Contributor
      • Feb 2007
      • 1251

      #3
      Originally posted by mrhoo
      Code:
      function locate(hoo){	
      	var T=0,L=0;
      	while(hoo){
      		L+= hoo.offsetLeft || 0;
      		T+= hoo.offsetTop || 0;
      		if(hoo== document.body) break;
      		hoo= hoo.parentNode;		
      	}
      	var A= [L,T];
      	return  A;
      }
      Thanks. Found the width and height elements too. I was surprised to find that they are not located with the style, but it makes sense to me now that I think about it.

      Thanks again.


      Adrian

      Comment

      Working...