No mater if the radio button is checked or not, .is(':checked') will return false. Here is some stuff I was playing with in Chrome's console while I was debugging this:
> $('#blah')
[<input type="radio" id="blah" name="blah_type" value="blah" class="styled" checked="checked">]
> $('#blah').removeAttr('checked')
[<input type="radio" id="blah" name="blah_type" value="blah" class="styled">]
> $('#blah').is(':checked')
false
> $('#blah').prop('checked', true)
[<input type="radio" id="blah" name="blah_type" value="blah" class="styled">]
> $('#blah').is(':checked')
false
> $('#blah').prop('checked')
true
> $('#blah').removeProp('checked')
[<input type="radio" id="blah" name="blah_type" value="blah" class="styled">]
> $('#blah').attr('checked', true)
[<input type="radio" id="blah" name="blah_type" value="blah" class="styled" checked="checked">]
> $('#blah').attr('checked')
"checked"
> $('#blah').prop('checked')
undefined
> $('#blah').is(':checked')
false
What could be going wrong here? I'm using jQuery 1.6.4.
No mater if the radio button is checked or not, .is(':checked') will return false. Here is some stuff I was playing with in Chrome's console while I was debugging this:
> $('#blah')
[<input type="radio" id="blah" name="blah_type" value="blah" class="styled" checked="checked">]
> $('#blah').removeAttr('checked')
[<input type="radio" id="blah" name="blah_type" value="blah" class="styled">]
> $('#blah').is(':checked')
false
> $('#blah').prop('checked', true)
[<input type="radio" id="blah" name="blah_type" value="blah" class="styled">]
> $('#blah').is(':checked')
false
> $('#blah').prop('checked')
true
> $('#blah').removeProp('checked')
[<input type="radio" id="blah" name="blah_type" value="blah" class="styled">]
> $('#blah').attr('checked', true)
[<input type="radio" id="blah" name="blah_type" value="blah" class="styled" checked="checked">]
> $('#blah').attr('checked')
"checked"
> $('#blah').prop('checked')
undefined
> $('#blah').is(':checked')
false
What could be going wrong here? I'm using jQuery 1.6.4.
Share Improve this question asked Aug 18, 2012 at 4:22 intargcintargc 3,1216 gold badges38 silver badges53 bronze badges 7- 1 Works fine here with 1.6.4: jsfiddle/j08691/wqhVQ – j08691 Commented Aug 18, 2012 at 4:29
- @arxanas nothing I can do about that. work requires this version for now. – intargc Commented Aug 18, 2012 at 4:30
- What happens if you rewrite it as .attr('checked', true)? – Vic Commented Aug 18, 2012 at 4:33
-
or maybe
$('#blah').attr('checked', 'checked')
– mask8 Commented Aug 18, 2012 at 4:34 -
Although this behaviour seems inconsistent, and makes for an interesting question, in your real code you should only set the checked status with
.prop('checked', trueOrFalse)
. Don't use.attr()
,.removeAttr()
or.removeProp()
. To test the status use$('#blah').prop('checked')
when you know the jQuery object has only one element (as when you select by id), or.is(":checked")
if you want to see if at least one of a larger set of elements is checked. – nnnnnn Commented Aug 18, 2012 at 4:36
2 Answers
Reset to default 5use .prop('checked')
.
I too faced the same problem. .is()
is not able to catch the checked status for a check box element.
According to the jQuery API docs, do not use .removeProp()
to remove checked
or other native properties. I would assume the same for .removeAttr()
as well.
http://api.jquery./removeProp/