jQuery mark Checkbox Checked on button click

 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

  $("#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:

Thank you for reading, pls keep visiting this blog and share this in your network. Also, I would love to hear your opinions down in the comments.

PS: If you found this content valuable and want to thank me? 👳 Buy Me a Coffee

Subscribe to our newsletter

Get the latest and greatest from Codepedia delivered straight to your inbox.

Post Comment

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