38

I want to get checkbox with specfic value and make it checked..

I make like this

$(":checkbox").filter({"value":5}).attr("checked","true");​

and here is the html

​<input type="checkbox" name="priv"​​​​​​​​​​​​​​​​​​​​​​​​​​​​​ value="1"​​​​​​​​​​​​​​​​​/>
<input type="checkbox" name="priv" value="2"/>
<input type="checkbox" name="priv" value="3"/>
<input type="checkbox" name="priv" value="4"/>
<input type="checkbox" name="priv" value="5"/>
<input type="checkbox" name="priv" value="6"/>
<input type="checkbox" name="priv" value="7"/>
 <input type="checkbox" name="priv" value="8"/>​

here's a demo of the problem

palAlaa
  • 9,500
  • 33
  • 107
  • 166

8 Answers8

110

You can use Attribute Equals Selector [name="value"] to get the checkboxes with particular value. Also use prop() instead of attr() as that is recommended way by jQuery doc.

Live Demo

$(":checkbox[value=4]").prop("checked","true");

or

$("input[type=checkbox][value=5]").prop("checked",true);

Description: Selects elements that have the specified attribute with a value exactly equal to a certain value, jQuery doc.

You can also do it Using attr() but prop is more appropriate for boolean properties.

$(":checkbox[value=4]").attr("checked","true");

As of jQuery 1.6, the .attr() method returns undefined for attributes that have not been set. To retrieve and change DOM properties such as the checked, selected, or disabled state of form elements, use the .prop() method, jQuery doc

Edit, asked in comments, "How would look like the answer/find-expression with complex values, like value="Smith, David"

You can enclose that kind of values like Smith, David on single quotes. You can even use character used by jQuery like #, ., $ etc, see updated fiddle here.

$("input[type=checkbox][value='#Smith, David$']").attr("checked","true");
Adil
  • 146,340
  • 25
  • 209
  • 204
  • Stole me half an hour: The semicolon in your second code example is kind of evil. When you copy the whole line (`$("input[type=checkbox][value=5]").prop("checked",true);`) and paste it in your script, SublimeText + JSHint (in my case) will throw the error 'Unexpected'. The browser **stops executing** your code with the error 'Unexpected token ILLEGAL'. Deleting the semicolon and replacing it with a "fresh" one helped me (imagine my debbuging desperation...) I guess there is a hidden ascii-char or sth – Cologne_Muc Apr 13 '16 at 09:51
  • 1
    Edit: sorry for double posting: `vim` showed me the little extra character: `$("input[type=checkbox][value=5]").prop("checked",true);<200b>` – Cologne_Muc Apr 13 '16 at 09:59
  • 1
    Sorry to know that the hidden character in the statement bothered you so much. I analyzed, traced and removed it. Thanks for pointing as it could save time of others. – Adil Apr 13 '16 at 10:11
  • I found character having code 8203, you can find more over here. http://stackoverflow.com/questions/2973698/whats-html-character-code-8203 – Adil Apr 13 '16 at 10:31
  • How would look like the answer/find-expression with complex values, like `value="Smith, David"`? – The Bndr Jul 13 '17 at 08:59
11
$(":checkbox").filter(function() {
  return this.value == '5';
}).prop("checked","true");​

DEMO

If you want to use it for different values then make it generic like following:

function checkWithValue(val) {
    $(":checkbox").filter(function() {
        return this.value == val;
    }).prop("checked", "true");
}


checkWithValue(5);​

DEMO

Alex V
  • 18,176
  • 5
  • 36
  • 35
thecodeparadox
  • 86,271
  • 21
  • 138
  • 164
4
$("input:checkbox[value='5']").attr("checked","true")
DaveShaw
  • 52,123
  • 16
  • 112
  • 141
bahaddin.yasar
  • 117
  • 1
  • 2
2

I had a dynamic values that needed to be looped over. This is what worked for me:

for (var i = 0, len = value.length; i < len; i++) {
  $("#form-name").find("input[type=checkbox][name='fName']").filter(function () {
    return this.value == values[i];
  }).prop("checked", true);
}

Hope that helps someone out.

Ray Hunter
  • 15,137
  • 5
  • 53
  • 51
1

It works also with square practise

 $(":checkbox").filter(["value":5]).prop("checked","true");
palAlaa
  • 9,500
  • 33
  • 107
  • 166
1

Sometimes we want to get checkbox dynamically for a specific value.

$(document).on('click','.at-filter a', function(){
    var aaa = $(this).parent().find("em").html();
    $("input[type=checkbox][value='"+aaa+"']").prop('checked',false)
});
Afraz Ahmad
  • 5,193
  • 28
  • 38
0
$(":checkbox[value=5]").attr("checked",true);​​​
Riz
  • 9,703
  • 8
  • 38
  • 54
0
$("#catalogTBL").find("input[type=checkbox][value='snet']").prop("checked", true);

This was the only way I could get this to work. Looking for a checkbox in a table.

pat capozzi
  • 1,412
  • 1
  • 20
  • 16