Uncheck radio button on click using jquery

26,967

Solution 1

Use .prop instead of .attr:

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

[Demo]

Solution 2

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.

Solution 3

If you use jQuery > 1.5, you should use prop instead of attr.

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

Demo here.

See here for details.

Solution 4

It's easy use following will help you:

$("input:checked").removeAttr("checked");

Check your updated Fiddle Here

Share:
26,967
DON
Author by

DON

I learn, unlearn and relearn continuously.

Updated on November 18, 2020

Comments

  • DON
    DON over 3 years

    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

  • Varun
    Varun over 8 years
    can you please create a JSFiddle for this ?