Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Radio Button Resets

Status
Not open for further replies.

nhoellei

Programmer
Feb 1, 2002
13
US
Is there anyway to reset/clear radio buttons, other than creating a generic reset button that resets all buttons withing the form? I have a table that contains two radio buttons per row so the user can select either button for that particular row. I have a "reset all" button but I would like to create a mechanism to clear a radio button only for the selected row.

Any ideas?
 
You can just set the radiobutton's checked property:

document.formName.radioName[index].checked = "0";

if you have 2 radio's:
<form name=f1>
<input type=&quot;radio&quot; name=&quot;choice&quot; value=&quot;val1&quot;> something
<input type=&quot;radio&quot; name=&quot;choice&quot; value=&quot;val2&quot;> something else
</form>

in order to clear the 2nd radio, add a button that will have onClick event:

<input type=&quot;button&quot; name=&quot;b2&quot; value=&quot;clear 2nd radio&quot; onclick=&quot;document.f1.choice[1].checked='0'&quot;>

That's it!
By thó way, you can do the same with checkboxes too.
 
Thanks - great info. I added some code to set the checked property(s) to &quot;0&quot;, but now the other radio selects. I wanted to mimic the reset button for the row rather than the entire form. Too bad what I want is not a simple as &quot;....checked.reset....&quot; X-)


Thanks for the reply.
 
X-) Wouldn't it figure, as soon as I send the reply to your post, I did some more tinkering and figured it out. What I did was create a button to pass the name of the radio buttons to the following function:

function uncheck(name) {

if (name[0].checked != &quot;0&quot;) {
name[0].checked = 0
}

if (name[1].checked != &quot;0&quot;) {
name[1].checked = 0
}

}


Thanks again! LOL
 
nhoellei:

Just to make
Code:
function uncheck()
more functional, you should have a loop:
Code:
function uncheck(name) {
 for (var i = 0; i < name.length; i++)
  name[i].checked = 0;
}
Note also, that if you're unchecking all of the elements, you don't need the condition
Code:
if (name[i].checked != &quot;0&quot;)

The function here will clear your checkboxes/radio buttons no matter how many there are in the form.
 
Also, if you don't rememebr what names are assigned to what from element types, you can add this:
Code:
function uncheck(name) {
 for (var i = 0; i < name.length; i++)

  if (name[i].type == &quot;radio&quot;)
    name[i].checked = 0;
}

This will prevent from clearing checkboxes along with radios.
This improvement is very helpful if you want to an action to be taken over some specific element type. Use this:
Code:
for text fields and textarea:
if (formname.elements[i].type == (&quot;text&quot; || &quot;textarea&quot;) || (&quot;password&quot;) )

for all kind of buttons:
if (formname.elements[i].type == (&quot;button&quot; || &quot;reset&quot; || &quot;submit&quot;) )

for checkbox:
if (formname.elements[i].type == &quot;checkbox&quot;)

for various selects:
if (formname.elements[i].type == (&quot;select-one&quot; || &quot;select-multiple&quot;) )
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top