user2249079
user2249079

Reputation:

Need Help To Uncheck Radio Button If Aleady Checked Using Jquery

i need help to uncheck or check same radio button if button already checked? I have jquery function which is used to enable or disabled other radio buttons if already checked now i want to add some function that use for check or uncheck same radio button?

(function ($) {
  $(document).ready(function () {
    $('input:radio').click(function(){
      var $inputs = $('input:radio')

      if($(this).is(':checked')){
        $inputs.not(this).prop('disabled',true);
      }else{
        $inputs.prop('disabled',false); 
      }
    })

  });
})(jQuery);

<input type="radio" value="Test1" />Test1
<input type="radio" value="Test2" />Test2
<input type="radio" value="Test1" />Test1
<input type="radio" value="Test2" />Test2

Upvotes: 4

Views: 11717

Answers (5)

Paul
Paul

Reputation: 46

The answer of Pablo Araya is good, but ...

$self.prop('checked', true);

is superfluous here. The radio button already has a checked state for every click.

Upvotes: 0

Pablo Araya
Pablo Araya

Reputation: 367

Although it is too late to answer, it could help others
I tried this solution.
Works very well for me!

$("input[type='radio'].myClass").click(function(){
        var $self = $(this);
        if ($self.attr('checkstate') == 'true')
        {
            $self.prop('checked', false);
            $self.each( function() {
                $self.attr('checkstate', 'false');
            })  
        }
        else
        {
            $self.prop('checked', true);
            $self.attr('checkstate', 'true');
            $("input[type='radio'].myClass:not(:checked)").attr('checkstate', 'false');
        }
})

Upvotes: 6

Mohamed AbdElRazek
Mohamed AbdElRazek

Reputation: 1684

I think you need checkbox instead of radio button to uncheck the checked :

<input class="checkDisable" type="checkbox" value="Test1" />Test1
<input class="checkDisable" type="checkbox" value="Test2" />Test2
<input class="checkDisable" type="checkbox" value="Test3" />Test3
<input class="checkDisable" type="checkbox" value="Test4" />Test4


(function ($) {
$(document).ready(function () {
 $('input:checkbox.checkDisable').change(function(){
    var $inputs = $('input:checkbox.checkDisable')
    if($(this).is(':checked')){
        $inputs.not(this).prop('disabled',true);
    }else{
        $inputs.prop('disabled',false); 
        $(this).prop('checked',false);
    }
 })

});
})(jQuery);

Upvotes: 0

karaxuna
karaxuna

Reputation: 26930

$inputs.filter(':checked').prop('checked', false);

Upvotes: 0

VisioN
VisioN

Reputation: 145398

Simply replace disabled with checked:

$input.prop("checked", false);

or for this element:

this.checked = false;

However, if you are looking for form element which can be checked and unchecked, maybe input type="checkbox" /> is what you need.

Upvotes: 2

Related Questions