How To Clear Radio Button In Javascript
9 Answers 9
You don't need to have unique id
for the elements, you can access them by their name
attribute:
If you're using name="Choose"
, then:
-
With jQuery it is as simple as:
$('input[name=Choose]').attr('checked',false);
-
or in pure JavaScript:
var ele = document.getElementsByName("Choose"); for(var i=0;i<ele.length;i++) ele[i].checked = false;
Demo for JavaScript
answered Mar 31 '10 at 15:44
NVRAMNVRAM
6,517 9 gold badges 38 silver badges 44 bronze badges
3
If you do not intend to use jQuery, you can use simple javascript like this
document.querySelector('input[name="Choose"]:checked').checked = false;
Only benefit with this is you don't have to use loops for two radio buttons
answered Mar 20 '18 at 15:56
shriroop_shriroop_
605 5 silver badges 15 bronze badges
This should work. Make sure each button has a unique ID. (Replace Choose_Yes and Choose_No with the IDs of your two radio buttons)
document.getElementById("Choose_Yes").checked = false; document.getElementById("Choose_No").checked = false;
An example of how the radio buttons should be named:
<input type="radio" name="Choose" id="Choose_Yes" value="1" /> Yes <input type="radio" name="Choose" id="Choose_No" value="2" /> No
answered Mar 31 '10 at 15:15
Shawn StewardShawn Steward
6,667 3 gold badges 23 silver badges 45 bronze badges
6
-
If i have one common id means, then how could i proceed?
Mar 31 '10 at 15:19
-
You need to change it to separate IDs. You should not have more than one of the same ID on a page. You need to use the same Name to make it a radio button group, but the ID has to be unique.
Mar 31 '10 at 15:19
-
@i2ijeya I would use a library such as jquery.com where I could select by class if I wanted to select more radio buttons at once. You could also use document.getElementsByName("Choose").
Mar 31 '10 at 15:27
-
While jQuery is great, it does not need to be used for every little bit of JavaScript. In this case it would just be unnecessary overhead for something that's pretty simple to do in plain JavaScript.
Mar 31 '10 at 15:31
-
As David says, you can access by name, so id isn't required for this function. Also, I suspect a typo -- your value is the same for both buttons.
Mar 31 '10 at 15:48
Wouldn't a better alternative be to just add a third button ("neither") that will give the same result as none selected?
answered Mar 31 '10 at 15:42
4
-
I quote this solution. Radio buttons, as a UI element, are not meant to be reset (ie: none of them checked). They are designed to start with 1 option checked, and the possibility to change it. You may consider to change your radio buttons to a dropdown list: {empty}|Yes|No
Mar 31 '10 at 15:52
-
@Filini Windows forms programming has 3-state radio buttons, not sure why they can't be used for the Web.
Apr 1 '10 at 14:05
-
@Shawn, are you sure you are not thinking about tri-state checkboxes? Anyway, how does this relate to the possibility to reset a radio button?
Apr 3 '10 at 17:14
-
@Filini - sometimes you might need to reset the radio buttons, all unchecked. I faced situation in a Quiz App - when user restarts the quiz, all the options (radio buttons) should be unchecked (created in AngularJS).
Jun 10 '16 at 3:49
An ES6 approach to clearing a group of radio buttons:
Array.from( document.querySelectorAll('input[name="group-name"]:checked'), input => input.checked = false );
answered Mar 25 '18 at 22:53
wLcwLc
820 10 silver badges 13 bronze badges
In my case this got the job done:
const chbx = document.getElementsByName("input_name"); for(let i=0; i < chbx.length; i++) { chbx[i].checked = false; }
answered Apr 8 '20 at 15:43
Avag SargsyanAvag Sargsyan
2,221 3 gold badges 26 silver badges 36 bronze badges
1
-
I'm having an issue with this js code. When you reset a radio groud, whenever I select the same value again, I get an undefined
May 9 at 22:59
Simple, no jQuery required:
<a href="javascript:clearChecks('group1')">clear</a> <script type="text/javascript"> function clearChecks(radioName) { var radio = document.form1[radioName] for(x=0;x<radio.length;x++) { document.form1[radioName][x].checked = false } } </script>
answered Mar 31 '10 at 15:46
if the id of the radio buttons are 'male' and 'female', value reset can be done by using jquery
$('input[id=male]').attr('checked',false); $('input[id=female]').attr('checked',false);
answered May 8 '18 at 6:28
YES<input type="radio" name="group1" id="sal" value="YES" > NO<input type="radio" name="group1" id="sal1" value="NO" > <input type="button" onclick="document.getElementById('sal').checked=false;document.getElementById('sal1').checked=false">
answered Mar 31 '10 at 15:17
SalilSalil
44.4k 20 gold badges 115 silver badges 150 bronze badges
2
-
That won't actually work if NO is checked because javascript will never see the 2nd element with the same ID.
Mar 31 '10 at 15:28
-
-1. It's invalid HTML to have multiple elements with the same ID.
Mar 31 '10 at 15:47
Not the answer you're looking for? Browse other questions tagged javascript html or ask your own question.
How To Clear Radio Button In Javascript
Source: https://stackoverflow.com/questions/2554116/how-to-clear-radio-button-in-javascript
Posted by: webbrespen.blogspot.com
Actually, this is what David Andersson suggested. I guess I missed his comment before I posted.
Mar 31 '10 at 15:47
+1 Its really working NVRAM.. Thanks and i am choosing this as a my accepted answer. Thanks everyone for your valuable answers.
Apr 1 '10 at 4:54
in jquery version above 1.6 use $('input[name=Choose]').prop('checked',false);
Jul 2 '19 at 9:19