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

button to deselect a radio button 2

Status
Not open for further replies.

tatika

MIS
Oct 6, 2003
35
US
Is there a way to code a radio button so that there are option to check and uncheck it.
I know this can be done w/check boxes, but I really would prefer to use the radio button instead (it looks better for the page design I am working on)

thanks in advance :)
 
Would you settle for
Code:
<input type=&quot;radio&quot; ondblclick=&quot;this.checked=false;&quot; />
?
 
you need to first validate (client scripting language) if the button is checked (true) or unchecked (false) then on that replation you can either set it to true for a unchecked button or false for a checked button.

javascript can do this fairly easy
eg on the pseudo
function checkUncheck(){
var statusCheck = document.myForm.radio;
if(statusCheck.checked == true) {
statusCheck.checked = false;
}

and so on..
tehre should be actual working examples out there for the pluking

_____________________________________________________________________
Where have all my friends gone to????
onpnt2.gif

 
Guys thanks for the help.

&quot;theboyhope&quot;s tip works really well. Too bad the option is to double click and not click only once to select and diselect the item.

&quot;onpnt&quot;s tip did not work probably because did not know exactly where to place the code:-(

 
Took a bit of fiddling, but try this in a new .html file:
Code:
<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot; &quot;[URL unfurl="true"]http://www.w3.org/TR/html4/loose.dtd&quot;>[/URL]
<html>
<head>
<title>test bed</title>
<script language=&quot;Javascript&quot;>
var lastPicked, lastCheckVal;
lastCheckVal = true;

function checkIt(objRadio) {
	//if this is the same radio button as was last clicked and was checked last time, deselect it
	if (objRadio == lastPicked && lastCheckVal) {
		objRadio.checked = false;
		lastPicked = 'nothing';
	}

	//update last-picked references
	lastPicked = objRadio;
	lastCheckVal = objRadio.checked;
}
</script>
</head>
<body>
<p>
<form name=&quot;ignoreThis&quot;>
click option again to deselect:<br> |
1 <input type=&quot;radio&quot; name=&quot;groupA&quot; value=&quot;1&quot; onclick=&quot;checkIt(this);&quot;> |
2 <input type=&quot;radio&quot; name=&quot;groupA&quot; value=&quot;2&quot; onclick=&quot;checkIt(this);&quot;> |
3 <input type=&quot;radio&quot; name=&quot;groupA&quot; value=&quot;3&quot; onclick=&quot;checkIt(this);&quot;> |
</form>
</p>
</form>
</body>
</html>

Tested IE6 & NN7

Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
any luck with this?

Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
Wouldn't it be easier just to use a checkbox?? If you click on a checkbox again it deselects without any code.
 
Radio buttons' default behaviour: max 1 selected in group.
Checkboxes: any amount can be selected in group.

But yes, for 'deselectability' checkboxes are friendlier :)

Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top