Find left position of table cell

Discussion in 'Javascript' started by ted, Jan 15, 2005.

  1. ted

    ted Guest

    Hi,

    Was wondering if it's possible to find the left position of a table cell if
    the cell is created dynamically.

    I have a table with a single cell. When a button is clicked, I'd like to add
    another cell to it, give the cell a class style and then find the cell's
    left position.

    Right now I'm using doing this:

    cell = document.createElement("td");
    cell.appendChild("sometext");
    cell.className = "cellStyle";
    tableRow = document.getElementById(theTableRowId);
    tableRow.appendChild(cell);

    How would I get the left position of each cell added?

    Any help appreciated. Thanks.

    -Ted
    ted, Jan 15, 2005
    #1
    1. Advertising

  2. ted wrote:


    > I have a table with a single cell. When a button is clicked, I'd like to add
    > another cell to it, give the cell a class style and then find the cell's
    > left position.
    >
    > Right now I'm using doing this:
    >
    > cell = document.createElement("td");
    > cell.appendChild("sometext");
    > cell.className = "cellStyle";
    > tableRow = document.getElementById(theTableRowId);
    > tableRow.appendChild(cell);
    >
    > How would I get the left position of each cell added?


    You can get the position of any element in the document the same way (in
    IE 4+, Netscape 6+, Mozilla, Opera 7+) by adding the offsetLeft and
    offsetTop values along the offsetParent hierarchy, e.g. assuming element
    is a reference to the element
    var coords = { x: 0, y: 0 };
    while (element) {
    coords.x += element.offsetLeft;
    coords.y += element.offsetTop;
    element = element.offsetParent;
    }
    // check coords.x, coords.y here
    Doesn't depend on whether it is a cell.


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Jan 15, 2005
    #2
    1. Advertising

  3. ted

    ted Guest

    Thanks Martin.

    "Martin Honnen" <> wrote in message
    news:41e93e2c$0$23139$-online.net...
    >
    >
    > ted wrote:
    >
    >
    >> I have a table with a single cell. When a button is clicked, I'd like to
    >> add another cell to it, give the cell a class style and then find the
    >> cell's left position.
    >>
    >> Right now I'm using doing this:
    >>
    >> cell = document.createElement("td");
    >> cell.appendChild("sometext");
    >> cell.className = "cellStyle";
    >> tableRow = document.getElementById(theTableRowId);
    >> tableRow.appendChild(cell);
    >>
    >> How would I get the left position of each cell added?

    >
    > You can get the position of any element in the document the same way (in
    > IE 4+, Netscape 6+, Mozilla, Opera 7+) by adding the offsetLeft and
    > offsetTop values along the offsetParent hierarchy, e.g. assuming element
    > is a reference to the element
    > var coords = { x: 0, y: 0 };
    > while (element) {
    > coords.x += element.offsetLeft;
    > coords.y += element.offsetTop;
    > element = element.offsetParent;
    > }
    > // check coords.x, coords.y here
    > Doesn't depend on whether it is a cell.
    >
    >
    > --
    >
    > Martin Honnen
    > http://JavaScript.FAQTs.com/
    ted, Jan 17, 2005
    #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. Martin
    Replies:
    3
    Views:
    1,492
    Martin
    Jun 22, 2006
  2. =?iso-8859-1?q?Jean-Fran=E7ois_Michaud?=

    Help on table align on left of page vs left hanging indent

    =?iso-8859-1?q?Jean-Fran=E7ois_Michaud?=, Jul 10, 2007, in forum: XML
    Replies:
    2
    Views:
    984
    =?iso-8859-1?q?Jean-Fran=E7ois_Michaud?=
    Jul 16, 2007
  3. Empire City
    Replies:
    3
    Views:
    387
    Empire City
    Apr 30, 2004
  4. Asra
    Replies:
    1
    Views:
    100
    Grant Wagner
    Oct 20, 2004
  5. Replies:
    7
    Views:
    187
    Lasse Reichstein Nielsen
    Jul 11, 2005
Loading...

Share This Page