-2

This doesn't work with jQuery 1.9

$("#" + IDProduct).attr("checked", "checked").checkboxradio("refresh");
RolandoCC
  • 868
  • 1
  • 14
  • 19
  • @Blender but as you can see in this modified fiddle, it's buggy when using `.attr`. http://jsfiddle.net/GYyef/1/ – Kevin B Mar 26 '13 at 17:53
  • exactly @Blender it's buggy when the checkbox us checked / unchecked several times via jQuery/javascript methods... – RolandoCC Mar 26 '13 at 17:56
  • possible duplicate of [jQuery 1.9 checkbox not checked second time after once it unchecked](http://stackoverflow.com/questions/14494467/jquery-1-9-checkbox-not-checked-second-time-after-once-it-unchecked) – Kevin B Mar 26 '13 at 17:57
  • There are so many duplicates of the proper way to get/set properties of an element - was this new Q/A really necessary? [First](http://stackoverflow.com/questions/9525128/jquery-set-radio-button), [Second](http://stackoverflow.com/questions/9871646/radio-button-checked-property), [Third](http://stackoverflow.com/questions/426258/how-do-i-check-a-checkbox-with-jquery-or-javascript), [Fourth](http://stackoverflow.com/questions/5874652/prop-vs-attr), [Fifth](http://stackoverflow.com/questions/15031831/check-uncheck-all-checkboxes-in-jquery-mobile-fails?rq=1) and many more – wirey00 Mar 26 '13 at 18:18

3 Answers3

9

to check it

$("#" + IDProduct).prop("checked", true).checkboxradio("refresh");

to un-check it

 $("#" + IDProduct).prop("checked", false).checkboxradio("refresh");

According to jQuery docs 1.9 version :

attr() versus .prop()

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
RolandoCC
  • 868
  • 1
  • 14
  • 19
  • 4
    Hi Rolando - .checkboxradio is not a part of the jQuery but is actually a jQuery mobile function so be careful because if jQuery mobile is not included then you will get undefined on the function – krilovich Mar 27 '13 at 17:46
  • This solved our problem where a "Select all" function would only select all once and uncheck all once and after that it would fail. After your fix, it works multiple times in sequence. Thanks. – Anriëtte Myburgh Apr 24 '13 at 08:23
  • if rad1 is currently checked and I programmatically check rad2, using `$("#rad2").prop("checked", true).checkboxradio("refresh");` then both radios appear checked. – pstanton Dec 01 '16 at 01:45
8

.checkboxradio("refresh");

is a jQuery mobile function so if you try to run it on an app that doesn't have jQuery mobile and is a jQuery only app it won't work.

Another thing I have noticed is that in many cases when you use .attr("checked",false) you will not be able to use .attr("checked",true) or .attr("checked","checked") afterwards on the same checkbox group .. this is just from personal experience

If you are using jQuery only then use .prop as RolandoCC has mentioned but keep in mind the checkboxradio does NOT work on jQuery checkboxes, ONLY on jQuery mobile specific items

** EDIT **
Another Tip that I can provide (this worked for me recently when ("checked",false) was breaking my checkboxes is to FIRST do ("checked",true) on the item that should be checked and only once that is checked to do ("checked",false) on the item you want to uncheck.. this way you can at least get the correct one checked

For example instead of doing:

$("#box1").attr("checked", false).checkboxradio("refresh");
$("#box2").attr("checked", true).checkboxradio("refresh");

do:

$("#box1").attr("checked", true).checkboxradio("refresh");
$("#box2").attr("checked", false).checkboxradio("refresh");

I am personally not sure why a checked false breaks any subsequent check true but maybe a bug needs to be logged against this.. good luck guys

kevinmicke
  • 5,038
  • 2
  • 20
  • 22
krilovich
  • 3,475
  • 1
  • 23
  • 33
  • Very helpful! This bug was killing me. I think your example is the wrong way around, which makes this a bit confusing. You forgot to switch #box2 and #box1 in the fixed version. – pythonjsgeo Jun 26 '14 at 00:58
  • 1
    But it looks like the official solution is to switch to prop() instead of attr() so perhaps you could highlight that more prominently. – pythonjsgeo Jun 26 '14 at 01:05
  • 1
    From what I remember back when I worked on this, prop did not work properly with the jQuery Mobile checkboxradio widget. – krilovich Jun 27 '14 at 18:13
5

to check it

$("#" + IDProduct).prop("checked", true).checkboxradio("refresh");

to un-check it

$("#" + IDProduct).prop("checked", false).checkboxradio("refresh");

status

$("#" + IDProduct).prop("checked") true/false

According to jQuery docs 1.7.1 version :

msnfreaky
  • 1,013
  • 10
  • 14