I have a table with a checkbox column - when you click the checkbox it performs a script that includes an AJAX request. I'm trying to set the checked state into a variable so I can then include this as a parameter in the AJAX POST request but I'm not able to get this into a variable so far.
Here's my checkbox input field with the script that is simplified to just capture the checked state of the input in the checkedState variable:
$(document).ready(function() {
$("input.select-item").click(function() {
var productID = $(this).val();
var checkedState = $("input.select-item").attr("checked");
console.log(checkedState);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
<td id="AT36288"><input type="checkbox" class="select-item checkbox" name="select-item" value="AT36288" /></td>
So if the checkbox was not checked and the user checks this I would like to capture the value of "checked" in the variable, and if the checkbox was checked and the user unchecked this I would like to capture the value of "uncheck" in the variable. I'm getting an undefined result for the variable in the console.
var checkedState = $("input.select-item").prop("checked");orvar checkedState = $("input.select-item").is(":checked");. And if you want to getchecked/uncheckstrings, you can use:var checkedState = $("input.select-item").is(":checked") ? "checked" : "uncheck";BUT passing boolean instead (true or false) seems more legit