Show / hide columns using CheckBoxes JQ
by Jason Doyle
HTML
<table id="tableId">
<thead>
<tr>
<th>
<input id="chk1" type="checkbox" /> Proj ID
</th>
<th>
<input id="chk2" type="checkbox" /> Project Name
</th>
<th>
<input id="chk3" type="checkbox" /> Disposition
</th>
<th>
<input id="chk4" type="checkbox" /> PRISM Release
</th>
<th>
<input id="chk5" type="checkbox" /> APP PM
</th>
</tr>
</thead>
<tbody>
<tr>
<td>
234567
</td>
<td>
col2
</td>
<td>
col3
</td>
<td>
col4
</td>
<td>
col5
</td>
</tr>
<tr>
<td>
234568
</td>
<td>
col2
</td>
<td>
col3
</td>
<td>
col4
</td>
<td>
col5
</td>
</tr>
<tr>
<td>
234569
</td>
<td>
col2
</td>
<td>
col3
</td>
<td>
col4
</td>
<td>
col5
</td>
</tr>
<tr>
<td>
234561
</td>
<td>
col2
</td>
<td>
col3
</td>
<td>
col4
</td>
<td>
col5
</td>
</tr>
<tr>
<td>
234562
</td>
<td>
col2
</td>
<td>
col3
</td>
...
CSS
th, td { border: 1px solid; padding: .2em .7em; }
JavaScript
$(function() {
$("#tableId input[type=checkbox]").on("change", function(e) {
var id = $(this).parent().index()+1,
col = $("table tr th:nth-child("+id+"), table tr td:nth-child("+id+")");
$(this).is(":checked") ? col.show() : col.hide();
}).prop("checked", true).change();
$("button").on("click", function(e) {
$("input[type=checkbox]").prop("checked", true).change();
});
})