0
I have this table that 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:
When you click on a button to preview it looks like this:
So far so good. This table is opened in a model
. When I click the 'ok' button to close the model
and I click again on the preview button:
That is, I in the 'Ok' button wanted to clean the whole table and then generate it again by clicking on preview to not replicate the data. I tried to put $('#table').remove();
but when I press preview again it gives me the following error:
Uncaught TypeError: Cannot set property 'className' of null
How can I clean the table and generate it again?
it would not be simpler if the table already had all the data, and when clicking on the checkbox only perform a toggleClass in the respective column?
– Tobias Mesquita
I’m quite Noob in HTML, I don’t know what toggleClass :
– Hugo Machado
something like: http://jsfiddle.net/8pxsymjv/
– Tobias Mesquita
I really liked your example, thanks in advance, it will certainly be useful. Just one question, the way it is, can the table appear after the checks selected? That is, I select Code and Designation, I click on a button and then the table appears , or toggleClasse only allows it to appear in real time ?
– Hugo Machado
in this case the event must be associated with the Click of this Button and not with the Click of the checkbox. toggleClass only "on/off" the class.
– Tobias Mesquita