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.
4 Answers 4
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.
answered Dec 23, 2015 at 9:56
Rory McCrossan
338k41 gold badges321 silver badges353 bronze badges
Sign up to request clarification or add additional context in comments.
3 Comments
Z. Clément
if old Jquery version use .attr()
Gogo
Thanks it worked, I have additional question. If I click on a link I want to set somehow that the id from link matches value from checkbox? $("a.type_link[id]").click(function (event) {$('input.type_checkbox[value="id"]').prop('checked', true);}
Rory McCrossan
No problem, glad to help. You'd be best to start a new question for that, as there's a fair bit more logic involved.
Use attribute equals selector:
$('input.type_checkbox[value=6]').prop('checked', true);
answered Dec 23, 2015 at 9:56
Milind Anantwar
82.3k26 gold badges97 silver badges128 bronze badges
Comments
try below code,it should be worked
$(":checkbox[value=6]").prop("checked","true");
or
$("input[type=checkbox][value=6]").prop("checked",true);
Alex Char
33.3k9 gold badges51 silver badges71 bronze badges
Comments
$("input[name='is_checked'][value='"+value+"']").prop("checked", true);
Comments
default