3

I've been developing a web application using bootstrap 3.0.2 and jQuery. But I've some problems with checkboxes. Imagine that I’ve the following checkbox:

<label class="checkbox margin-left5">
<input type="checkbox" name="xptoName" id="xptoNameId">
Hello World
</label>

So i want to check/uncheck through jQuery. For that, I use this function:

function checking(isToCheck, id){
 $("#"+id).attr('checked', isToCheck);
}

If you call the first time this function:

checking(true, "xptoNameId");

It'll check the checkbox, but if I called several times after this, for checking and unchecking the checkbox will not check again.

Any clue for this strange behavior?

Thanks.

Juan Jardim
  • 2,232
  • 6
  • 28
  • 46
  • possible duplicate of [Check checkbox checked property using jQuery](http://stackoverflow.com/questions/901712/check-checkbox-checked-property-using-jquery) – zessx Nov 29 '13 at 17:05

1 Answers1

5

Use .prop() instead of .attr()

function checking(isToCheck, id) {
    $("#" + id).prop('checked', isToCheck);
}

Read .prop() vs .attr()

Community
  • 1
  • 1
Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107