I'm trying to create a users' permission table which will display the type of permission to assign a user, based on a selection from a checkbox.
I have tried several means I know about and also searched but I always seem to get stuck everytime. I got a similar code from this answer, jQuery append and remove values from checkbox to textarea , which works. But when I try implementing it in my work, it doesn't seem to work.
<table id="permissionsTable">
<thead>
<th>Name</th>
<th>Slug</th>
<th>Description</th>
</thead>
<tbody id="tableBody">
</tbody>
</table>
<div id="alloptions">
<ul>
<li>
<input type="checkbox" name="option1" value="Play" />Play</li>
<li>
<input type="checkbox" name="option1" value="Create" />Create</li>
<li>
<input type="checkbox" name="option1" value="Delete" />Delete</li>
</ul>
</div>
And here's the jQuery Code:
var checkboxes = $("input[type='checkbox']");
var permsTable = $("#permissionsTable");
var valuer = [];
checkboxes.on('change', function() {
var values = checkboxes.filter(':checked').map(function(x, y) {
return y.value;
}).get();
checkboxes.filter(':checked').each(function(a, b) {
valuer.push($(this).val());
});
if (checkboxes.is(':checked')) {
permsTable.find('tbody').detach();
permsTable.append($('<tbody>'));
$.each(valuer, function(c, d) {
//alert(x + " : " + y);
var nameCol = d.substr(0, 1).toUpperCase() + d.substr(1) + " " + "Game";
var slugCol = d.toLowerCase() + "-" + "Game";
var descriptionCol = "Allow user to " + d.toUpperCase() + " a " + "Game";
$("#permissionsTable").find('tbody').append("<tr><td>" + nameCol + "</td><td>" + slugCol + "</td><td>" + descriptionCol + "</td></tr>");
});
} else {
permsTable.find('tbody').detach();
permsTable.append($('<tbody>'));
btnGeneratePermissions.css("display", "none");
}
});
I expect to add the values of the checkboxes to the table row when checkbox is checked; and then remove the row upon uncheck.