jQuery if checkbox is checked
if ($('input.checkbox_check').is(':checked')) {
for jQuery 1.6 or higher:
if ($('input.checkbox_check').prop('checked')) {
//blah blah
}
the cross-browser-compatible way to determine if a checkbox is checked is to use the property https://api.jquery.com/prop/
this $('#checkboxId').is(':checked')
for verify if is checked
& this $("#checkboxId").prop('checked', true)
to check
& this $("#checkboxId").prop('checked', false)
to uncheck
If none of the above solutions work for any reason, like my case, try this:
<script type="text/javascript">
$(function()
{
$('[name="my_checkbox"]').change(function()
{
if ($(this).is(':checked')) {
// Do something...
alert('You can rock now...');
};
});
});
</script>
If checked:
$( "SELECTOR" ).attr( "checked" ) // Returns ‘true’ if present on the element, returns undefined if not present
$( "SELECTOR" ).prop( "checked" ) // Returns true if checked, false if unchecked.
$( "SELECTOR" ).is( ":checked" ) // Returns true if checked, false if unchecked.
Get the checked val:
$( "SELECTOR:checked" ).val()
Get the checked val numbers:
$( "SELECTOR:checked" ).length
Check or uncheck checkbox
$( "SELECTOR" ).prop( "disabled", false );
$( "SELECTOR" ).prop( "checked", true );