2

This seems like it would have a really obvious answer, however I cannot get a simple checkbox to uncheck with JQuery once I add Jquery Mobile.

Here is some very simple code as an illustration:

<html> <head> <title>Untitled</title> <link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.css" /> <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script> <script src="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.js"></script> <script> jQuery(function () { jQuery('#a').change(function () { if (jQuery(this).is(':checked')) { jQuery('#b').removeAttr('checked'); } }); jQuery('#b').change(function () { if (jQuery(this).is(':checked')) { jQuery('#a').removeAttr('checked'); } }); }); </script> </head> <body> <input type="checkbox" id="a"><label for="a">AAAA</label> <input type="checkbox" id="b"><label for="b">BBBB</label> </body> </html> 

This is a simple bit of code. When Checkbox 'A' gets checked, it should uncheck checkbox 'B', and vise verse. However it doesn't, and no error is written to the console.

This is using the standard version of Jquery & Jquery Mobile, linked directly to their site

If I remove the Jquery Mobile elements, by removing the following lines, then it works fine

<link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.css" /> <script src="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.js"></script> 

Can anyone please enlighten me as to where I am going wrong, since this seems so simple yet I'm at an absolute loss as to what is going on.

Many thanks

1
  • try obj.checked = false; (no jquery) Commented May 16, 2013 at 5:19

1 Answer 1

3

Because you need to use .prop and then re-apply styles using .checkboxradio('refresh').

Check

$('selector').prop('checked', true).checkboxradio('refresh'); 

Uncheck

$('selector').prop('checked', false).checkboxradio('refresh'); 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much. I had actually already tried .prop, without any success. The difference seems to be the '.checkboxradio('refresh').

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.