Radio buttons are unchecked only at page refresh
<input type="radio" name="test">
1<input type="radio" name="test">2
<input type="button" id="btn" />
$("#btn").click(function(){
$(':radio').each(function () {
$(this).removeAttr('checked');
$('input[type="radio"]').attr('checked', false);
})
});
I have created a fiddle http://jsfiddle.net/8jKJc/220/
But its not working with Bootstrap
To set a radio button to checked/unchecked, select the element and set its checked property to true or false , e.g. myRadio. checked = true . When set to true , the radio button becomes checked and all other radio buttons with the same name attribute become unchecked. Here is the HTML for the examples in this article.
One solution is to assign on mousedown the value you are going to assign to it (the opposite of what it has) in a variable on the node like this. __rval and check for its existence in your onclick handler. If it exists, you know the value in it is correct, though the this.
click(function () { $('input[type=radio]'). removeAttr('checked'); $(this). find('input[type=radio]'). attr('checked', 'checked'); });
Use .prop
instead of .attr
:
$('input[type="radio"]').prop('checked', false);
[Demo]
Use this :
$(this).prop('checked', false);
//$('input[type="radio"]').attr('checked', false);
You can see this link for differentiate between .prop()
with .attr()
. .attr()
suitable for accessing HTML element attributes like(name, id, class,etc..)
and .prop()
for DOM element properties that returned boolean value for most case.
From jQuery official page :
For example, selectedIndex, tagName, nodeName, nodeType, ownerDocument, defaultChecked, and defaultSelected should be retrieved and set with the .prop() method. Prior to jQuery 1.6, these properties were retrievable with the .attr() method, but this was not within the scope of attr. These do not have corresponding attributes and are only properties.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With