Connecting Tech Pros Worldwide Help | Site Map

attribute like offsetTop but showing the absolute distance

Pf
Guest
 
Posts: n/a
#1: Jul 23 '05
A piece of html code:

<table>
<tr>
<td>
<input type="text" name="myInput" onclick="myFunction(this)">
<td>
</tr>
</table>

myFunction(el) {
alert(el.offsetTop)
}

Alert shows the distance from input to the top of the table or table cell.
Is there any other attribute which shows the absolute distance from input to
the top of the page ? Because of some reasons I can't add table's and
input's offsetTops.

Best regards,
ABS


Martin Honnen
Guest
 
Posts: n/a
#2: Jul 23 '05

re: attribute like offsetTop but showing the absolute distance




Pf wrote:
[color=blue]
> A piece of html code:
>
> <table>
> <tr>
> <td>
> <input type="text" name="myInput" onclick="myFunction(this)">
> <td>
> </tr>
> </table>
>
> myFunction(el) {
> alert(el.offsetTop)
> }
>
> Alert shows the distance from input to the top of the table or table cell.
> Is there any other attribute which shows the absolute distance from input to
> the top of the page ? Because of some reasons I can't add table's and
> input's offsetTops.[/color]

You can add offsetTop along the offsetParent hierarchy e.g.
var el = ...
var totalOffsetTop = 0;
while (el != null) {
totalOffsetTop += el.offsetTop;
el = el.offsetParent;
}
I don't think there is a way without adding offsetTop values of
individual elements.


--

Martin Honnen
http://JavaScript.FAQTs.com/
shlomi.schwartz@gmail.com
Guest
 
Posts: n/a
#3: Jul 23 '05

re: attribute like offsetTop but showing the absolute distance


Try this:
/**
* returns the absolute left location of an element.
* param: e: element
* return: an integer representing the offset from left.
*/
function getElementLeftPosition(e){
var x=0;
while(e){
x+=e.offsetLeft+e.clientLeft;
e=e.offsetParent;
}
return x;
}

/**
* returns the absolute top location of an element.
* param: e: element
* return: an integer representing the offset from top.
*/
function getElementTopPosition(e){
var y=0;
while(e){
y+=e.offsetTop+e.clientTop;
e=e.offsetParent;
}
return y;
}

Pf
Guest
 
Posts: n/a
#4: Jul 23 '05

re: attribute like offsetTop but showing the absolute distance


shlomi.schwartz@gmail.com wrote:[color=blue]
> Try this: [...][/color]

Thanks a lot. Could you tell me what is e.clientLeft for ? I got NaN as
returned value till I didn't remove it.

ABS


shlomi.schwartz@gmail.com
Guest
 
Posts: n/a
#5: Jul 23 '05

re: attribute like offsetTop but showing the absolute distance


clientLeft Property:

Retrieves the distance between the offsetLeft property and the true
left side of the client area.

Closed Thread


Similar JavaScript / Ajax / DHTML bytes