Click to See Complete Forum and Search --> : get the position of <div> ?


ProfEich
07-27-2003, 06:47 AM
I wrote the following in my HTML code:


<DIV ID="TestA" STYLE="position: absolute;">AAAA</DIV>
<DIV ID="TestB" STYLE="position: absolute;">BBBB</DIV>


Later I tried to set positions. (This is only an example - In real I try something like this one ^^)


document.all.TestA.style.left = document.all.TestB.style.left + 20;


But it won't work. How can I get the Position of this ones in veriables like TestAX and TestAB that I can add them to another Integer variable?

[Edit] It seems as if the left position of TestB is zero. But I like to get the position on my HTML page.

Cya
eich

Nevermore
07-27-2003, 06:56 AM
Before you try anything you should note thatJavaScript can't change variable that deosn't exist, so you have to set the position in CSS before you change it with JavaScript.

The code would be
document.getElementById('TestA'.style.left=

ProfEich
07-27-2003, 07:23 AM
that's the problem! I can't fit the position!
It should be within a HTML page at any position. The position is there where the text ends for this user (maybe he or she has a different text size in his/her browser)

If there would a constant position the tables (which I have in my DIV's) would be in front (or behind) the text at a wrong position.

I'm programming some kind of color choose-table for a registration-page for my online-RPG and this table shouldn't be at the position for Nicknames ;)

Nevermore
07-27-2003, 11:32 AM
Why not use relative positioning on the DIV, so that it just fits in like a normal element? Or set the size of the frame with the text in so that you know where it ends.

Jesus
09-25-2003, 12:26 AM
Hello, I've got exactly the same problem...
I need to know the (x,y) position of a <div> with a particuliar ID.. Is there really no way to do this in Javascript? This seems strange to me, because one can know the coordinates of the window, so why not of the elements inside ?

I would be so glad to have an answer.

Khalid Ali
09-25-2003, 08:08 AM
You can get the location of the element with regards to its parent element using
divEl.offsetLeft
divEl.offsetTop

What happens is that you will get the div's left or top posiotion with reference to its immidiate parent element,but if yu keep getting the offset for the next parent and add it to the previous value eventually you will get the divs position with ref to the html documents left corner.

Jesus
09-25-2003, 08:43 AM
ok so I just have to write a recursive function and that will do.
Thank you :)

Khalid Ali
09-25-2003, 08:47 AM
Originally posted by Jesus
.... have to write a recursive function and that will do.
Thank you :)

:D
Sweeeet..you got it my friend and you are welcome..