8

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.

asked Dec 23, 2015 at 9:55
0

4 Answers 4

9

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
Sign up to request clarification or add additional context in comments.

3 Comments

if old Jquery version use .attr()
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);}
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.
2

Use attribute equals selector:

$('input.type_checkbox[value=6]').prop('checked', true);
answered Dec 23, 2015 at 9:56

Comments

1

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
answered Dec 23, 2015 at 10:08

Comments

0
$("input[name='is_checked'][value='"+value+"']").prop("checked", true);
answered Jun 26, 2024 at 9:25

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.