find position of a element in a table

Discussion in 'Javascript' started by louvino@gmail.com, Mar 22, 2007.

  1. Guest

    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.
    , Mar 22, 2007
    #1
    1. Advertising

  2. wisestpotato Guest

    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.
    wisestpotato, Mar 22, 2007
    #2
    1. Advertising

  3. Guest

    On Mar 22, 10:24 am, "wisestpotato" <>
    wrote:
    > 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.
    , Mar 22, 2007
    #3
  4. On Mar 22, 2:17 am, wrote:
    > 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
    scripts.contact, Mar 22, 2007
    #4
  5. wisestpotato Guest

    On 22 Mar, 17:08, "scripts.contact" <> wrote:
    > On Mar 22, 2:17 am, wrote:
    >
    > > 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


    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.
    wisestpotato, Mar 26, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eshrath Ali Khan
    Replies:
    1
    Views:
    827
    Joris Gillis
    Nov 9, 2004
  2. TS
    Replies:
    5
    Views:
    3,655
    Steven Cheng[MSFT]
    Aug 16, 2007
  3. Konrad Hammerer
    Replies:
    14
    Views:
    596
    Jonathan N. Little
    Feb 7, 2008
  4. Angus
    Replies:
    10
    Views:
    866
    Jonathan Lee
    Mar 11, 2011
  5. Knut
    Replies:
    1
    Views:
    120
    George Hester
    Oct 15, 2004
Loading...

Share This Page