Restyling a table using JavaScript is a straightforward task. Let's say your table is named your-table
. Simply select the table, move the last cell to a new row, and insert it after the current row.
When adding rows to the table, remember to start at the end of the table to avoid an infinite loop. Using
for(var i=0; i<rows.length; i++)
inappropriately can lead to this issue.
Fiddle: http://jsfiddle.net/QP8ga/
function updateTable(table){
var rows = table.rows;
for(var i=rows.length-1; i>=0; i--){
var tr = document.createElement("tr");
var td = rows[i].cells[2];
td.colSpan = "2";
tr.appendChild(td);
rows[i].parentNode.insertBefore(tr, rows[i].nextSibling)
}
}
window.onload = function(){
var table_IDs = ["your-table", "another-table"];
for(var i=0; i<table_IDs.length; i++) updateTable(table_IDs[i]);
}
The code has been adjusted to handle multiple tables. The provided fiddle demonstrates a simple example with one table.