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

Radiobutton event handling

Status
Not open for further replies.

katbeme

Programmer
Sep 18, 2008
4
US
I am trying to figure out how to clear a textbox field when a radiobutton list is deselected. So I have 3 radiobuttons and textbox field and say when user check Radiobutton#1 and enters value in the textbox but then decides they wnat to select Radiobutton#2 how to clear the value out of textbox that is in Radiobutton#1. I tried using onchange, onfocus events but they don't seem to work.
 
Using onclick seems to work OK:

Code:
<html>
<head>
	<script type="text/javascript">

		function updateText() {
			var frm = document.forms['theForm'].elements;
			var radios = frm['theRadios'];
			if (radios[0].checked) {
				frm['theText'].value = 'Radio 1';
			} else if (radios[1].checked) {
				frm['theText'].value = 'Radio 2';
			}
		}

	</script>
</head>

<body>
	<form name="theForm">
		Radio 1 <input type="radio" name="theRadios" value="1" onclick="updateText();" />
		<br />
		Radio 2 <input type="radio" name="theRadios" value="2" onclick="updateText();" />
		<br />
		<input type="text" name="theText" value="" />
	</form>
</body>
</html>

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top