Dan
Dan

Reputation: 2837

Basic jQuery Selector Issue

I'm trying to select a checkbox with value "foo" and check it. I tried this:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>$("input[type='checkbox'][value='foo']").attr('checked', true);</script>

But nothing seems to be happening. Is there something I'm missing? I'm very new at this.

Upvotes: 2

Views: 48

Answers (2)

KAKHA13
KAKHA13

Reputation: 305

you can do like this $(":checkbox[value='foo']").prop("checked","true");

<script>
$(function(){
$(":checkbox[value='foo']").prop("checked","true");
});
</script>

Upvotes: 0

scniro
scniro

Reputation: 16989

Could you be calling this before your .ready() event? This example works fine

<input type="checkbox" value="foo" />
<input type="checkbox" value="notFoo" />

$(function() {
    $("input[type='checkbox'][value='foo']").attr('checked', true);
});

JSFiddle Example

So esentially, in your example if you wish to keep it looking as is, just wrap it as such

<script>$(function() { $("input[type='checkbox'][value='foo']").attr('checked', true); })</script>

Upvotes: 2

Related Questions