Search code examples
jqueryhtml

How to set checkbox with a certain value checked in jquery?


I have HTML elements:

<input class="type_checkbox" id="1" name="types[]" type="checkbox" value="1">
<input class="type_checkbox" id="0" name="types[]" type="checkbox" value="6">

I want to set checkbox true with jQuery where value is equal to 6. Pls help.


Solution

  • You can use the attribute selector along with prop() to set the checked property.

    $('input.type_checkbox[value="6"]').prop('checked', true);
    

    You can obviously amend the input.type_checkbox part of the selector to better suit your needs.