JavaScript

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.

6 thoughts on “Checking/unchecking checkbox doesn’t work in jQuery mobile”

Leave a reply

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