Checking/unchecking checkbox doesn’t work in jQuery mobile

I’m using an old school jQuery code to modify elements that has true/false attributes such as checked or selected. This appears to be not working when I tried it on jQuery mobile checkbox. I used a different method instead and it works so far without investigation why my old method does not work.

My old code goes like this.

// To check
$("#some_checkbox").attr("checked", "checked").checkboxradio("refresh");

// To uncheck
$("#some_checkbox").removeAttr("checked").checkboxradio("refresh");

This method, for some reason does not work for me in jQuery mobile (simulated in a desktop browser). I saw a sample in docs and tried to use it and it works flawlessly.

// To check
$("#some_checkbox").prop("checked", true).checkboxradio("refresh");

// To uncheck
$("#some_checkbox").prop("checked", false).checkboxradio("refresh");

Note that this may require higher version of jQuery. However, using:

$("#some_select").attr("selected", "selected").selectmenu("refresh");

works for select/dropdown elements. Weird.

I haven’t investigated further but there must be something wrong with my old code. Too lazy to investigate though.

This entry was posted in JavaScript and tagged , , , . Bookmark the permalink.

Related Posts

6 Responses to Checking/unchecking checkbox doesn’t work in jQuery mobile

  1. Cheb says:

    Good Post! Thx

  2. ferose says:

    Super really helped me. Thanks a lot

  3. Sam says:

    Nice post,Thanks.

  4. pinki says:

    Thanks!! Its really helping.

  5. WinterWych says:

    Just had to thank you after many hours of frustrating checkbox coding fails!

  6. JJ says:

    Great ! I was getting mad with these checkboxes not displaying the new state.

Leave a Reply

Your email address will not be published. Required fields are marked *