I am completely new to JavaScript, so, sorry for asking this simple question. The point is that there have been numerous similar questions here and on other platforms. I've tried all solutions provided but neither of them worked in my case. I assume that this is because of my CSS, otherwise I do not know what is the reason. I tried different methods: insertRow, clone etc. neither of them worked.
The point is that my program needs to add row or column to the table on click on the buttons. Right button should add column to the right of the table, bottom button should add row to the bottom of the table.
// append row to the HTML table
function appendRow() {
var tbl = document.getElementById('my-table'), // table reference
row = tbl.insertRow(tbl.rows.length), // append table row
i;
// insert table cells to the new row
for (i = 0; i < tbl.rows[0].cells.length; i++) {
createCell(row.insertCell(i), i, 'row');
}
};
// create DIV element and append to the table cell
function createCell(cell) {
var div = document.createElement('div'), // create DIV element
cell.appendChild(div); // append DIV to the table cell
};
// append column to the HTML table
function appendColumn() {
var tbl = document.getElementById('my-table'), // table reference
i;
// open loop for each row and append cell
for (i = 0; i < tbl.rows.length; i++) {
createCell(tbl.rows[i].insertCell(tbl.rows[i].cells.length), i, 'col');
}
};
#canvas {
align-content: center;
height: 1000px;
}
#my-table {
margin: 0 -2px -2px 0;
border: #FFF;
border: 1px solid rgb(72, 170, 230);
display: inline-block;
}
tr {
background: rgb(72, 170, 230);
}
td {
width: 50px;
height: 50px;
}
.add {
height: 52px;
width: 52px;
background: rgb(243, 165, 0);
cursor: pointer;
color: #FFF;
text-align: center;
font-family: 'Open Sans', sans-serif;
transition-property: background;
transition-duration: 1s;
transition-timing-function: linear;
}
#addColumn {
display: inline-block;
vertical-align: top;
margin: 2px 0 0 0;
}
.add:hover {
background: rgb(246, 192, 82);
}
#addColumnChild {
line-height: 50px;
}
#addRow {
vertical-align: bottom;
margin: 0 0 0 2px;
}
#addRowChild {
line-height: 50px;
}
.del {
height: 52px;
width: 52px;
background: rgb(178, 0, 0);
cursor: pointer;
color: #FFF;
text-align: center;
font-family: 'Open Sans', sans-serif;
transition-property: background;
transition-duration: 1s;
transition-timing-function: linear;
}
#delColumn {
display: inline-block;
vertical-align: top;
margin: 2px 0 0 0;
}
.del:hover {
background: rgb(202, 76, 73);
}
#delColumnChild {
line-height: 50px;
}
#delRow {
vertical-align: left;
margin: 0 0 0 2px;
}
#delRowChild {
line-height: 50px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<title>Test task 1</title>
<body>
<!-- <div id="delColumn" class="del" onclick="appendColumn">
<div id="addColumnChild"><b>–</b></div>
</div>
<div id="delRow" class="del" onclick="appendRow()">
<div id="delRowChild"><b>–</b></div>
</div>-->
<table id="my-table">
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</table>
<div id="addColumn" class="add" onclick="appendColumn">
<div id="addColumnChild"><b>+</b></div>
</div>
<div id="addRow" class="add" onclick="appendRow()">
<div id="addRowChild"><b>+</b></div>
</div>
How to make buttons work and actually add rows and columns?
JavaScriptsolution? If I provide solution withjQuerywill be fine?