When using JavaScript to copy a table cell, I encountered an issue where the style was not being copied along with the content. Attempting to directly assign the style from one cell to another did not work as expected.
To ensure that the text-align property was being copied correctly, I had to explicitly set it like this: newCell.style.textAlign = oldCell.style.textAlign;
While this method worked for individual styles, it became cumbersome to remember to add each new style manually. This led me to wonder if there was a way to loop over all the style properties and copy them.
In Chrome, I was able to achieve this by dynamically accessing the style attributes of a cell and applying them to a new cell:
var strAttribute = GetDomNameFromAttributeName(oRow.cells[1].style[0]);
var styletocopy = eval('oRow.cells[1].style.'+strAttribute);
eval("newCell.style."+strAttribute+"='"+styletocopy+"'");
Unfortunately, this approach did not work in Internet Explorer. My compatibility testing in Firefox is pending.
Does anyone know of a way to effectively loop through the style elements in Internet Explorer? Alternatively, is there a more efficient method to copy all style properties at once?