Cells move because this is what you say. You define the table as follows:
<tr> <td colspan="2">cell 1</td> <td>cell 2</td> </tr> <tr> <td>cell 3</td> <td>cell 4</td> </tr>
So, the shift that you see, I expect.
If you want to merge cells, you will need to take the contents of all merged cells, merge them into one cell and delete the rest. For a trivial example, it would look like this:
function setColSpan() { var myTable = document.getElementById('myTable'); var cell2html = myTable.rows[0].cells[1].innerHTML; myTable.rows[0].deleteCell(1); myTable.rows[0].cells[0].innerHTML = myTable.rows[0].cells[0].innerHTML + ' ' + cell2html; myTable.rows[0].cells[0].colSpan = 2; }
However, a more reliable solution is a complex process. Especially if you want the ability to unfold. You would need to somehow store the information about the old cell structure ... maybe something like <span class="oldCell">cell 2</span> around the merged data? And then checking for the existence of "oldCell" covers when it does not work? But then you have to save this information with the help of user rights. And find out what it means to merge rows and columns. And also find out what this means for overlapping mergers.
spinn source share