Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

html

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.

like image 938
Gogo Avatar asked Dec 23 '15 09:12

Gogo


People also ask

How do I mark a checkbox as checked in jQuery?

Reading a Checkbox When using jQuery and you wish to read whether a checkbox is checked or not. $('#checkboxid').is(':checked'); This will return true if the checkbox is checked and false if left unchecked.

How can get checkbox value in variable in jQuery?

To get the value of the Value attribute you can do something like this: $("input[type='checkbox']"). val();


2 Answers

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.

like image 68
Rory McCrossan Avatar answered Oct 13 '22 00:10

Rory McCrossan


Use attribute equals selector:

$('input.type_checkbox[value=6]').prop('checked', true);
like image 36
Milind Anantwar Avatar answered Oct 13 '22 01:10

Milind Anantwar