I have this table which will be generated with values of checkboxes
that I select.
HTML:
<table class="table table-hover" id="table">
<thead>
<tr></tr>
</thead>
<tbody>
<tr id="Row2"></tr>
<tr></tr>
<tr></tr>
</tbody>
</table>
JavaScript:
$('#preVisualizar').click(function () {
var checkbox = $('input:checkbox[name^=mcheckbox]:checked');
if (checkbox.length > 0) {
var val = [];
checkbox.each(function () {
val.push($(this).val());
});
var context = document.getElementById("table");
context.className = "table";
var header = context.createTHead();
var row = header.insertRow();
for (var i = 0; i < val.length; i++) {
var th = document.createElement('th');
th.innerHTML = val[i];
row.appendChild(th);
var row2 = document.getElementById("Row2");
var y = row2.insertCell(i);
y.innerHTML = "Ex. " + val[i];
};
} else {}
}
});
WEB:
Clickingonabuttontopreviewitlookslikethis:
So far so good. This table opens in model
. When I click on the 'ok' button to close the model
and click again on the preview button it looks like this:
That is, I in the 'Ok' button wanted to clear the whole table and then re-generate it by clicking preview so I did not replicate the data. I tried to put $('#table').remove();
but when I reload in preview it gives me the following error:
Uncaught TypeError: Cannot set property 'className' of null
How can I clean the table and regenerate it?