Find left position of table cell

T

ted

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
 
M

Martin Honnen

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.
 
T

ted

Thanks Martin.

Martin Honnen said:
ted wrote:



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.
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top