laura
laura

Reputation: 2961

Is it possible to deselect all values in a RadioButton Group?

I have a form in an ASP.NET MVC site which the user can edit and return to at a later time.

If they accidentally select a value in a radio button group, is there any way to make it unselected?

By default, once it's clicked, it can't be unclicked!

Upvotes: 1

Views: 5219

Answers (2)

JustinStolle
JustinStolle

Reputation: 4420

You can do it with plain . Example:

function clearRadios(groupName) {
  var r = document.getElementsByName(groupName);
  for (var i = 0; i < r.length; i++) {
    r[i].checked = false;
  }
}
<label><input type="radio" name="myGroup">A</label>
<label><input type="radio" name="myGroup">B</label>
<label><input type="radio" name="myGroup">C</label>
<button type="button" onclick="clearRadios('myGroup')">Clear</button>

Upvotes: 5

Lazarus
Lazarus

Reputation: 43084

You have two options, create another radio button labelled "None" or something appropriate to your form that's part of the same 'group' then they can select that instead.

But in reality, if you need them to be able to deselect then you need a checkbox and not a radio button.

Upvotes: 1

Related Questions