Click here to Skip to main content
15,900,378 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to add a table row or table column at specific index by on clicking the table cells, same as excel sheet.

What I have tried:

HTML
<table >
  <tr>
    <th></th>
    <th></th>
    <th></th>
  </tr>
  <tr>
    <td></td>
    <td></td>
    <td></td>
  </tr>
</table>


script to add row:

JavaScript
function AppendRows() {
        var tableRows = document.getElementById('myTable'),
            row = tableRows.insertRow(tableRows.rows.length);
        row.height = '50';
        for (var i = 0; i < tableRows.rows[0].cells.length; i++) {
            row.insertCell(i), i, 'row';
        }
    }
Posted
Updated 17-Oct-18 16:42pm

1 solution

I wrote this just for you!
<HTML>
<STYLE>
.space {
	background:url('data:images/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAAMSURBVBhXY5h58xUABGsCXQMuL3UAAAAASUVORK5CYII=');
	height:5px;
	cursor: pointer;
}
</STYLE>
<SCRIPT>
function onrow_click(t) {
	var currentRowIndex = t.rowIndex;
	if(currentRowIndex % 2 != 0) {
		return;
	}
	var pickIndex;
	if(t.parentElement.rows.length == t.rowIndex + 1) {
		pickIndex = currentRowIndex - 1;
	} else {
		pickIndex = currentRowIndex + 1;
	}

	var col_count = t.parentElement.rows[pickIndex].cells.length; //
	var newRow = t.parentElement.insertRow(currentRowIndex + 1);
	for(var i=0;i<col_count;i++) {
		var newCol = newRow.insertCell();
		newCol.innerHTML="Bold";
		
	}
	var newEmptyRow = t.parentElement.insertRow(currentRowIndex + 2);;
	var newEmptyCol = newEmptyRow.insertCell();
	newEmptyCol.colSpan = col_count;
	newEmptyCol.className = "space";
	newEmptyCol.onclick = onclick="onrow_click(this)";
}
</SCRIPT>
<BODY>
	<TABLE id='dd' border=1 cellpadding=0 cellspacing=0>
		<TR class="space" onclick="onrow_click(this)">
			<TD colspan=3></TD>
		</TR>
		<TR>
			<TD>TEST 1</TD>
			<TD>TEST 2</TD>
			<TD>TEST 3</TD>
		</TR>
		<TR class="space"  onclick="onrow_click(this)">
			<TD colspan=3></TD>
		</TR>
		<TR>
			<TD>TEST 4</TD>
			<TD>TEST 5</TD>
			<TD>TEST 6</TD>
		</TR>
		<TR class="space" onclick="onrow_click(this)">
			<TD colspan=3></TD>
		</TR>
	</TABLE>
</BODY>
</HTML>
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900