X, Y position problem

Discussion in 'ASP .Net' started by Shahar, Nov 12, 2003.

  1. Shahar

    Shahar Guest

    Hi

    I have some div in a td that in some table, something like that:

    <table>
    <tr>
    <td>
    <div id="div">some text</div>
    </td>
    </tr>
    </table>

    I need to get the x,y position of the div from a script like that:
    <script>
    alert(div.style.left);
    </script>

    but I get an empty string, I can get the x, y only if I write it from
    advance, like that:
    <div id="div" style="left:50;">some text</div>

    is there anyway to get the x,y without setting it from advance ?

    Thanx.
     
    Shahar, Nov 12, 2003
    #1
    1. Advertising

  2. Shahar

    bruce barker Guest

    thats because style.left returns the text attribute of the style if set. you
    want the offsetLeft. this is the offset from the objects parent. this
    function will give the absolute left

    findPosX(obj) {
    var curleft = 0;
    if (document.getElementById || document.all) {
    while (obj.offsetParent) {
    curleft += obj.offsetLeft
    obj = obj.offsetParent;
    }
    }
    else if (document.layers)
    curleft += obj.x;
    return curleft;
    }




    "Shahar" <> wrote in message
    news:...
    > Hi
    >
    > I have some div in a td that in some table, something like that:
    >
    > <table>
    > <tr>
    > <td>
    > <div id="div">some text</div>
    > </td>
    > </tr>
    > </table>
    >
    > I need to get the x,y position of the div from a script like that:
    > <script>
    > alert(div.style.left);
    > </script>
    >
    > but I get an empty string, I can get the x, y only if I write it from
    > advance, like that:
    > <div id="div" style="left:50;">some text</div>
    >
    > is there anyway to get the x,y without setting it from advance ?
    >
    > Thanx.
     
    bruce barker, Nov 12, 2003
    #2
    1. Advertising

  3. Shahar

    Shahar Guest

    thanx Bruce,
    that's what I need
    ;-)
     
    Shahar, Nov 13, 2003
    #3
    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. Luqman
    Replies:
    1
    Views:
    659
    Luqman
    Feb 7, 2006
  2. James Wong
    Replies:
    4
    Views:
    508
    James Wong
    Jul 14, 2004
  3. Replies:
    3
    Views:
    186
  4. James Black
    Replies:
    0
    Views:
    404
    James Black
    May 28, 2006
  5. brendan
    Replies:
    0
    Views:
    200
    brendan
    Aug 29, 2006
Loading...

Share This Page