find position of a element in a table

L

louvino

Hi,

my html code is :

<div style="position:absolute;top:10px;left:10px">
<table>
<tr>
<td id="TdBarreBouton1">
<hr>
</td>
</tr>
<tr>
<td id="TdBarreBouton2">
<button id="btn_inserer">Bouton</button>
</td>
</tr>
</table>
</div>

I want to know the postion of the button.

My javascript code is :

boutonInserer = document.all.btn_inserer;
pos_left = document.all.TdBarreBouton2.offsetLeft +
boutonInserer.offsetLeft;
pos_top = document.all.TdBarreBouton2.offsetTop +
boutonInserer.offsetTop;

But, the problem is if I move the button in the TD (id=TdButton1), I
MUST change the javascript code.
Is it possible to calculate the position of the button without
indicating the name of the TD in the javascript code ?

Thanks.
 
W

wisestpotato

You can use "parentNode" to get the parent TD of your button. E.g.

var boutonInserer = document.getElementById("btn_inserer");
var TdBarreBouton2 = boutonInserer.parentNode;

regards,

wp.
 
L

louvino

You can use "parentNode" to get the parent TD of your button. E.g.

var boutonInserer = document.getElementById("btn_inserer");
var TdBarreBouton2 = boutonInserer.parentNode;

regards,

wp.

Thanks.

It's ok.
 
S

scripts.contact

Hi,
I want to know the postion of the button.

My javascript code is :

boutonInserer = document.all.btn_inserer;
pos_left = document.all.TdBarreBouton2.offsetLeft +
boutonInserer.offsetLeft;
pos_top = document.all.TdBarreBouton2.offsetTop +
boutonInserer.offsetTop;

use offsetParent
 
W

wisestpotato

use offsetParent

Be wary of offsetParent. It returns the parent element with respect to
positioning. So, if the current element is absolutely positioned, the
offsetParent may well be the document root node. If you want to be
sure of getting the parent node of the current element, irrespective
of any stylesheet positioning rules, then use parentNode instead.

wp.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top