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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Clear/Uncheck all radio buttons.. 1

Status
Not open for further replies.

RhythmAddict112

Programmer
Jun 17, 2004
625
US
Hi all...Is there a way for me to unCheck all radio buttons in a form? I need to trick this event onClick of a table cell....So basically, clear all radio buttons in form1 triggered by this onCLick event. The radio buttons have different names - however, I'm not sure if that kind of kills the entire idea or not..As I could only find sample code to clear out radio buttons of the same name when googling this.

thanks, any ideas on this appreciated

Use your resources, you're on the internet!
 
Radio buttons within a group should all have the same name, that is what makes them exhibit the radio behavior. However you can have different groups of radio buttons and the groups can have different names. Is that what you mean?


Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Hi Ts,

My apologies for being unclear. I have several(15 or so) groups of radio buttons that get rendered by my .net page. Im tryin to avoid a trip back to the server to uncheck all the radio buttons...And I can't reset the form (because of other form elements)



Use your resources, you're on the internet!
 
This should get it done:
Code:
<html>
<head>
<title>uncheck all</title>
<Script type='text/javascript'>
function uncheckAll(f)
{
for (x=0;x<f.elements.length;x++)
	{
	if (f.elements[x].type == "radio")
	{
	f.elements[x].checked = false; 
	}
	}
}
</script>
</heaD>
<body>
<form name="form1"><input type='button' onClick='uncheckAll(this.form)'>
<input type='radio' name='r1'>
<input type='radio' name='r1'>
<input type='radio' name='r1'><BR>
<input type='radio' name='r2'>
<input type='radio' name='r2'>
<input type='radio' name='r2'><BR>
<input type='radio' name='r3'>
<input type='radio' name='r3'>
<input type='radio' name='r3'>
</form>
</body>
</html>
</form>

If you need it explained or anything else, please feel free to ask.

"It is the mark of an educated mind to be able to entertain a thought without accepting it." - Aristotle
 
elegidito, many things as that works perfectly.
It looks like youre just running through all the elements that are of type "radio" and then just unchecking them if they are in fact radio. Elegant. I had tried to do something, somewhat simliar before posting by using "getElementByType("radio")" however the "object doesn't support this method..." error.

This was my exact code..

Code:
  allChk = document.getElementByType("radio");
	 for (i = 0; i < allChk.length; i++) 
		{
		document.Form1.allChk[i].checked = false;
		}

many things again for your help!

Use your resources, you're on the internet!
 
There's no such thing as getElementByType. You could use getElementsByTagName("input") as in:

Code:
allInp = document.getElementByTagName("input");
for (i = 0; i < allInp.length; i++) 
{
  if (allInp[i].type == "radio")
  {
    allInp[i].checked = false; 
  }
}
 
Well that would explain why it doesn't work :)

thank you everyone, as usual I learn a ton from these boards.

Use your resources, you're on the internet!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top