-1

im trying to uncheck a jquerymobile checkbox with jquery. im trying to uncheck it when a person clicks on a button.

this is me checkbox:

<input id='_inputcb_Q801' name='_inputcb_Q801' type='checkbox'>
<label id='_inputcb_Q801L' for='_inputcb_Q801'>Text</label>

and i have tried those options.

function uncheckCB(){
$('#_inputcb_Q801').prop('checked',false).checkboxradio('refresh');
$('#_inputcb_q801').not(:checked);
$('input[type=\"checkbox\"]:first').attr('checked', false).checkboxradio('refresh');
$('input[name=_inputcb_Q801]').attr('checked',false);
$('input[name=_inputcb_Q801]').prop('checked', false);
}

the function is a onclick of a button.

none of the above options work. anyone knows another way to uncheck a jquerymobile checkbox.

thanks in advance

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
user1652050
  • 153
  • 3
  • 6
  • 16

2 Answers2

2

This works:

$("#btn")
    .click( 
        function(){
           $('input[name=_inputcb_Q801]').prop('checked',false);
        });

Probably it doesn't work for you because you have javascript errors. Try to inspect your code in browser. ​See in action here: http://jsfiddle.net/eH7Fn/2/
Please also read on .prop() vs .attr()

Edit:
the jsfiddle you've provided (http://jsfiddle.net/pjottard/97uMb/) doesn't work for several reasons:
1.you used mootools as a library, but you need to select Jquery
2. you didn't define function: priceCounterCB();
3. you didn't provide href attribute for your link to be clickable. you need to add href="#" to your link or better yet use button.
working example: http://jsfiddle.net/97uMb/1/

Community
  • 1
  • 1
RAS
  • 3,375
  • 15
  • 24
  • @Sushanth--, you are correct, although I think that attr('checked',false). is the cleanest solution. – RAS Nov 15 '12 at 07:53
  • I don't think so .. better to ID and .prop() instead..As the ID is unique it will be the fastest as internally it uses document.getElementById.. checked is a property of the element and not a attribute.. – Sushanth -- Nov 15 '12 at 07:54
  • @Sushanth--, I think you're right. Just read about differences here: http://stackoverflow.com/questions/5874652/prop-vs-attr thanks for you suggestion, I will update the answer. – RAS Nov 15 '12 at 07:58
  • the solution you gave me is not working in jquerymobile. any other suggestions ? .prop isnt working either. – user1652050 Nov 15 '12 at 08:05
  • @user1652050, have you looked at jsfiddle? what exactly doesn't work? Can you give us an example in jsfiddle of the non-working example? – RAS Nov 15 '12 at 08:07
  • hi, i've seen your edit, i have the function in me complete code. it's not a link but a jquerymobile button, i guess thats why it doesnt work. the solution you gave me still doesn't work the cb stays checked. i've got a form that have to be filled in. at the end i show all the answers the gave me, then they can change it if they want. the cb that needs to be unchecked is on the next page. not on the page with the button. maybe that's the problem ? – user1652050 Nov 15 '12 at 08:45
  • i have found the problem but not the solution..... when i have just the checkbox, your solution is working. but when i add a label to it it stops working, and stays checked. any idea why that is ? – user1652050 Nov 15 '12 at 09:21
  • @user1652050, the only thing that can happen is that you have error in your javascript code. Can you show non-working example again with your new setup? The solution I gave you is using a label and working fine. – RAS Nov 15 '12 at 17:28
  • the problem is that the gui doesnt update. if i use your code snippet it does uncheck but in the gui it's still checked. and if i put .checkboxradio('refresh') at the end it gives a error. – user1652050 Nov 16 '12 at 07:58
1

found the solution, ill post it below it might help someone:

  $('#_bQ801').click(function(){
        $.mobile.changePage('#Question_09_01', 'slide', false, false);
        $('#_inputcb_Q901').attr('checked', false);
        $('#_inputcb_Q901').checkboxradio('refresh');}
user1652050
  • 153
  • 3
  • 6
  • 16