Go Back   CodingForums.com > :: Client side development > JavaScript programming > DOM and JSON scripting

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-14-2006, 02:15 AM   PM User | #1
CWG
New to the CF scene

 
Join Date: May 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
CWG is an unknown quantity at this point
Get the absolute position of elements in the DOM

I have some div tags that I want to capture the xy coordinates using javascript. It seems that if I do not preset the absolute positioning values I cannot access them.

For example I have a div tag nested in a table:

<table>
<tr>
<td width="400">some other code</td>
<td width="380">
<div Id="DivId">div content</div>
</td>
</tr>
</table>

You will notice the positioning of the div tag is drive by the table it is nested in. The value of document.getElementById('DivId').style.left and document.getElementById('DivId').style.right is null. It sems I can only get these values of they are preset.

If someone could enlighten me with the solution I would be greate appreciative.
CWG is offline   Reply With Quote
Old 05-14-2006, 02:38 AM   PM User | #2
Kravvitz
Senior Coder

 
Join Date: Feb 2006
Location: USA
Posts: 1,013
Thanks: 0
Thanked 0 Times in 0 Posts
Kravvitz is an unknown quantity at this point
Read these:
http://www.quirksmode.org/dom/getstyles.html
http://www.quirksmode.org/js/findpos.html
__________________
Learn CSS. | SSI | PHP includes | X/HTML Validator | CSS validator | Dynamic Site Solutions
Java != JavaScript && JScript != JavaScript
Design/program for Firefox (and/or Opera), apply fixes for IE, not the other way around.
Kravvitz is offline   Reply With Quote
Old 05-14-2006, 05:25 PM   PM User | #3
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
I should add that you must probably consider the scroll position as well...
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Old 05-20-2006, 07:22 AM   PM User | #4
mrhoo
Regular Coder

 
Join Date: Mar 2006
Posts: 708
Thanks: 30
Thanked 127 Times in 118 Posts
mrhoo will become famous soon enoughmrhoo will become famous soon enough
Code:
function whereAmI(who,wch){
	var L=0, R=0;
	var pa=who;
	while(pa.parentNode){
		L+= ( pa.offsetLeft)? pa.offsetLeft: 0;
		R+= (pa.offsetTop)?  pa.offsetTop: 0;	

		if(pa==document.body || wch===false)break;
		pa= pa.parentNode;
		
	}
	var A=[L,R];
	return(wch===1 || wch=== 2)?  A[wch]: A;
}
This returns the left and top coordinates of the element sent
as the first argument. If the second argumentis a literal false,
it returns the position relative to its parent, otherwise it returns the
position relative to the body (includes the body offsets as well).

You can send an integer (1 or 2) if you just want the
first(left) or second(right) coordinate.
mrhoo is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:12 AM.


Advertisement
Log in to turn off these ads.