Tell me how to make sure that by clicking again on the selected radio button, it again becomes not cheked. I understand that I was wrong somewhere in the code

$(document).ready(function(){ $('input[type="radio"]').click(function(){ var ir = $(this).prop("checked") if(ir){ $(this).removeAttr("checked") } }) }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input id="check1" type="radio" name="group1" value="val1" checked> <label for="check1">Text1</label> <input id="check2" type="radio" name="group2" value="val2"> <label for="check2">Text2</label> <input id="check3" type="radio" name="group3" value="val3"> <label for="check3">Text3</label> 

  • Well cheked! = Checked =) But it works only once - alexoander
  • yes that's the problem) - ChromeChrome
  • Well, in general, there are checkboxes for this purpose, but if you need to cut it like this, then you probably should add logic to add checked, because you immediately demolish it by clicking (the default value is triggered before your handler) - alexoander
  • I’m freezing at all, tell me - ChromeChrome

1 answer 1

 $(document).ready(function(){ $('input[type="radio"]').click(function(){ $(this).attr('checked', function(index, attr){ return attr ? null : 'checked'; }); }) }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input id="check1" type="radio" name="group1" value="val1"> <label for="check1">Text1</label> <input id="check2" type="radio" name="group2" value="val2"> <label for="check2">Text2</label> <input id="check3" type="radio" name="group3" value="val3" cheked> <label for="check3">Text3</label>