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!

HOW TO ASSIGN OPTION FROM SELECT BOX TO THE TEXTAREA

Status
Not open for further replies.

Coding

Programmer
Aug 13, 2000
25
0
0
CA
Can anyone help me how to assign option from SELECT box
by onChange event when I click on any item in the select box and want it to appear on the TEXTAREA at the and of the existing text.
For example:
when I type:
Dear Mr.
and click on the SELECT box to pick some option like in this case FirstNameLastName and I get this option value in the TEXTAREA like this: Dear Mr. FirstNameLastName
Here is my start code:
Code:
<script language=&quot;javascript&quot;>
function swapOptions(the_name)
{
	var numbers_select = window.document.the_form.txtComment.value;
	var theName = eval(the_name);
	setOptionText(window.document.the_form.txtComment.value, theName);
}

function setOptionText(the_select, theName)
{
	the_select.options.text.value = theName;
}
</script>
<FORM name=&quot;the_form&quot;>

<select name=&quot;choose_category&quot; onChange=&quot;swapOptions(window.document.the_form.choose_category.options[selectedIndex].text.value);&quot;>
	<option selected>dogs
	<option>fish
	<option>birds
</select>

<TEXTAREA name=&quot;txtComment&quot; rows=2 cols=60 size=&quot;100&quot;>
 
before I help you out you should understand that you selected the wrong icon. The one you selected is the Helpful hint icon and your post is a question. Doing such things are bad netiquette. Do tell us you didn't mean to make such a mistake! :) Gary Haran
 
Make the following change sin your code:

1. remove &quot;window.&quot; everywhere (for example in window.document.the_form.txtComment.value;)

2. You don't need any of your swapOptions() and setOptionText() functions.

3. The argument in <select onChange=&quot;&quot;> event handler has errors, but you don't need it.

4. add this instead:

<select name=&quot;choose_category&quot; onChange=&quot;document.the_form.txtComment.value+=this.options[this.selectedIndex].text&quot;>
<option selected>Adams
<option>Smith
<option>Last_Name
</select>



You can also make that both 1st and last names are added:

<select name=&quot;choose_category&quot; onChange=&quot;addName(this)&quot;>
<option value=&quot;Tom&quot; selected>Adams
<option value=&quot;Bill&quot;>Smith
<option value=&quot;First_Name&quot;>Last_Name
</select>

function addName(person) {

document.the_form.txtComment.value += this.options[this.selectedIndex].value + &quot; &quot; this.options[this.selectedIndex].text;
}

I made a new function in order to make it look more pretty.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top