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

unchecking radio buttons

Status
Not open for further replies.

SKTodd

Programmer
Mar 11, 2002
33
US
I want a radio button to become unchecked when the user clicks on it a second time. Click on it once to make it checked; click on it again to uncheck it. What is the script to accomplich this?
 
Radio buttons are designed to be used in clusters where you can select only one item out of a choice (like the old car radios). The sort of action you're describing belongs more properly to a checkbox control.

Behold:

Code:
<!DOCTYPE html 
     PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot;
     &quot;[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;>[/URL]

<html xmlns=&quot;[URL unfurl="true"]http://www.w3.org/1999/xhtml&quot;[/URL] xml:lang=&quot;en&quot; lang=&quot;en&quot;>
  <head>
    <meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot;></meta>
    <title>JavaScript Sample</title>
  </head>
  <body>
    <form name=&quot;MainForm&quot;>
      <p>Choose the shape of the Destructor:<br />
      <input type=&quot;radio&quot; name=&quot;OidarMaster&quot; id=&quot;Oidar1&quot;></input>Chocolate<br />
      <input type=&quot;radio&quot; name=&quot;OidarMaster&quot; id=&quot;Oidar2&quot;></input>Caramel<br />
      <input type=&quot;radio&quot; name=&quot;OidarMaster&quot; id=&quot;Oidar3&quot;></input>Nougat<br />
      <input type=&quot;radio&quot; name=&quot;OidarMaster&quot; id=&quot;Oidar4&quot;></input>An ear-splitting all-consuming Shriek of Doom</p>
      <p>Do you wish the Destructor to ravage Tokyo as well as your hometown?<br />
      <input type=&quot;checkbox&quot;></input>Ravage Tokyo, too!</p>
      <p>Have a nice day.</p>
    </form>
  </body>
</html>

Beware!
[monkey] Edward [monkey]

&quot;Cut a hole in the door. Hang a flap. Criminy, why didn't I think of this earlier?!&quot; -- inventor of the cat door
 
Suppose the user discovers the radio button that was selected should not have been selected at all? How can it become unselected?
 
<script>
function checkIt(inButton){
if (inButton.checked){
inButton.checked = false
}
else{
inButton.checked = true
}
}
</script>

<input type=radio onCLick=&quot;checkIt(this)&quot;> Get the Best Answers! faq333-2924
Is this an asp FAQ? faq333-3048
Tek-Tips Best Practices: FAQ183-3179
It's sad that some Americans proudly turn their backs on the very Flag the gives them the opportunity to do just that... - Mike
 
Suppose the user discovers the radio button that was selected should not have been selected at all? How can it become unselected?

That's my point. If the User must choose at least one item from a list of 2+ items, then a radio button is appropriate. If you have only a single selection, or if no-choice is possible, then a checkbox is the more appropriate control to use.

If your situation is such that you have exactly one choice and the user can choose it or unchoose it, then you use a checkbox.

mwolf00's post looks like it'll take a radio button and force it to work like a checkbox. You could likewise take a group of checkboxes and force them to act like radio buttons. But it would be easier to use the right control for the right function.

Unless you really like extra coding... [smile]

Cheers,
[monkey] Edward [monkey]

&quot;Cut a hole in the door. Hang a flap. Criminy, why didn't I think of this earlier?!&quot; -- inventor of the cat door
 
Thanks Ed for setting me straight on this. I appreciate your help! You too mwolf00!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top