I am currently trying to build a list with checkboxes that can be easily checked/unchecked to be included or excluded. When the page loads most of the list items will have the checkbox "checked" attribute to show that the item is included. Im trying to change the checkbox attribute when the user changes the selection so that I can save the results.
I Have tried a few combinations that I have found here on Stackoverflow but none are changing the attribute, Im not sure what Im doing wrong.
function check() {
if ($("#working").prop("checked", true)) {
$("#working").prop("checked", false)
} else {
$("#working").prop("checked", true)
}
}
var data = {
id: document.getElementById('workingId').value,
visible: document.getElementById('working').checked
};
$(document).on("click", ".save", function() {
alert(data);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="hidden" id="workingId" value="13245">
<input type="checkbox" name="working" id="working" onclick="check(this);" checked> I am a check box
<button type="button" class="btn save"> Save</button>
I am hoping to print an array that has an ID for the checkbox (12345) and whether the checkbox is now checked/unchecked. Any help with this would be greatly appreciated.