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!

How do i populate a textbox from a radio button? 1

Status
Not open for further replies.

chdavisjr

Programmer
Jan 24, 2002
85
US
I've had a complete brain shut-down today. Any and all help would be appreciated on this "sorta" urgent problem!

I have 2 radio buttons and a text box. Here is a very simplized version of my problem.

If I click Option 1 I want the value "Red" in the text box.
If I click Option 2 I want the word "Blue" in the text box.
(for example)

() Option 1
() Option 2

You selected the color: [_________]

The form starts out blank, until the user selects one of the 2 radio buttons. As soon as they click their choice (Using, I am sure the JavaScript attribute: OnClick() can be used to assign the textbox value.
I am sure the solution is of the form:
Code:
<Form name=form1>
<input type=radio name=radio1 value="red"
    onClick="form1.color.value="red"> Option 1
<BR>
<input type=radio name=radio1 value="blue"
    onClick="form1.color.value="blue"> Option 2
<BR>
You select the color: <input type=text value="">
<BR>
</form>
Thanks,
Chalmers
 
you're very close; i'd do it this way:

Code:
<script language='javascript'>
   function showColor(param) {
      document.formName.textField.value = param;
   }
</script>

then, in the radio buttons:

Code:
<input type='radio' name='radio1' value='one' onclick='showColor("red");'>Red<br />
<input type='radio' name='radio1' value='two' onclick='showColor("blue");'>Blue

Always code as if the person who ends up maintaining your code will be a violent psychopath who knows where you live. -- Martin Golding


 
Thank you, tigerjade!

I just "found" the solution!
Here is what I did:
Code:
<form name=form1>
<input type=radio name=radio1 value="red" onClick="form1.color.value='red'"> Option 1
<BR>
<input type=radio name=radio1 value="blue" onClick="form1.color.value='blue'"> Option 2
<BR>
You select the color: <input name=color type=text value="">
<BR>
</form>

I had quotes (["]) within quotes (["])!!
Is there an advantage (or disadvantage) as to whether I use a function as opposed to using the javascript attribute, as I did?
Thanks again!
Chalmers
 
congrats, chalmers! :) the only advantage is that you can reuse the function elsewhere, and it allows you to be a bit lazier in typing. i thoroughly subscribe to the policy of conserving finger energy wherever possible :)

tigerjade

Always code as if the person who ends up maintaining your code will be a violent psychopath who knows where you live. -- Martin Golding


 
Thank you much, Martin, for your fast replies.
I can always depend on the members of Tek-Tips to provide help when it is needed!

Good point about "reusablility"!
Chalmers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top