Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncheck radio button on click using jquery

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

like image 994
DON Avatar asked Aug 24 '15 09:08

DON


People also ask

How do you make a radio button unchecked by clicking it?

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.

How do you checked a radio button when a button is clicked?

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.

How remove checked attribute from radio in jQuery?

click(function () { $('input[type=radio]'). removeAttr('checked'); $(this). find('input[type=radio]'). attr('checked', 'checked'); });


2 Answers

Use .prop instead of .attr:

$('input[type="radio"]').prop('checked', false); 

[Demo]

like image 157
Mohit Kumar Avatar answered Sep 22 '22 16:09

Mohit Kumar


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.

like image 30
Norlihazmey Ghazali Avatar answered Sep 20 '22 16:09

Norlihazmey Ghazali