How to Check if Checkbox is Checked in jQuery?

If you want to get true/false depending on whether the checkbox is checked, then I recommend using the following syntax: $(“#testCheckbox”).is(“:checked”);

:checked Selector

The :checked selector works for checkboxes, radio buttons, and options of select elements.

.is() Filtering

Check the current matched set of elements against a selector, element, or jQuery object and return true if at least one of these elements matches the given arguments.

Set True/False depends on the checkbox – jQuery Example

Here is a simple example. On the page we have a label, checkbox, and span. By clicking on the checkbox we need to set ‘true’ or ‘false’ into the span tag.
jquery checkbox checked true false

The full HTML and jquery code:

<!DOCTYPE html>
<html>
<head>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function(){
        
            $("#testCheckbox").click(function(){
            
            var isChecked = $("#testCheckbox").is(":checked");
            
            $("#checkBoxState").text(isChecked);
            });
        });
    </script>
</head>
<body>
    <h1>Get Checkbox Value</h1>
    
    <label for="testCheckBox">Test CheckBox</label>
    
    <input id="testCheckbox" name="testCheckBox" type="checkbox" />
    
    <span id="checkBoxState"></span>
</body>
</html>

 

Watch my Video with Example

More topics in this section:

Leave a Reply

Your email address will not be published.