jQuery mark Checkbox Checked on button click

satinder singh / Updated : / 0 Comments

jQuery set checkbox as checked: Here in this article we will learn how in jQuery we can mark our checkboxes as checked whenever user click on specific button. jQuery is a fast, small, and features rich JavaScript library. It makes things like HTML document traversal and manipulation easy.

So today we will see if we have a checkbox in our HTML page (form element). And wants to marked that checkbox as checked depend upon some value or condition. Then by using .prop() method we able to achieve the same.

.Prop() : The prop() method sets or returns properties and values of the selected elements.


Let’s see the code, how it will work.

# Html Code: Add button and a checkbox.

Here we added a checkbox and give it id as myCheckbox, along with a input button tag having id as myButton.
Now on button click we make the checkbox as checked.


<input type="checkbox" id="myCheckBox"/> My Checkbox
<input type="button" id="myButton" value="Click Me" /> 


# jQuery Code: Use .prop() method to make checkbox checked


$("#myButton").on('click',functino(){
  $("#myCheckBox").prop("checked", true); 
});

Here in above code on button click, we have use .prop() method and set attribute checked as true, which marked as checked. If we set attribute checked as false then this will unchecked the checkbox.

You can also check how to unchecked the checkbox in jquery with live example.

Conclusion: Here in this article we able to change our checkbox values. That is by using .prop() method we set our checkbox checked as well as unchecked.

# Other Resource:

Hope you enjoyed this tutorial. If you have any recommendations, please let us know what you think in the comment section below! See you again next time!


Post Comment


Your email address will not be published. Required fields are marked *