Solved

Insert value from dropdown into textarea onclick

Posted on 2009-05-15
8
303 Views
Last Modified: 2012-05-07
I have a form (see code snippet) that has a dropdown menu and a textarea. When someone selects something from the dropdown I want to insert it into the textarea.

How would I do this?
<form id="form1" action="" method="post">
	<select name="options">
		<option>Select a template</option>
		<option>Nobody at home</option>
		<option>Something else</option>
	</select>
	<textarea rows="15" cols="57" name="notes"></textarea><br />
	<input type="submit" value="">
</form>

Open in new window

0
Comment
Question by:synergiq
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
8 Comments
 
LVL 2

Accepted Solution

by:
divstar earned 250 total points
ID: 24394794
Try this javascript function.

The parameter dropdown is the id of the dropdown menu to be chosen from
The parameter textbox is the id of the textbox to be inserted into

In this example I have ran the function when the menu changes using the onchange attribute


...
<head>
<script type="text/javascript">
function setTextbox(dropdown,textbox)
{
	document.getElementById(textbox).value =      document.getElementById(dropdown).value;
}
</script>
</head>
 
<body>
<form id="form1" action="" method="post">
        <select name="options" id="options" onchange="setTextbox('options','notes');">
                <option>Select a template</option>
                <option>Nobody at home</option>
                <option>Something else</option>
        </select>
        <textarea rows="15" cols="57" name="notes" id="notes"></textarea><br />
        <input type="submit" value="Submit">
</form>
</body>
...

Open in new window

0
 
LVL 10

Assisted Solution

by:bugada
bugada earned 250 total points
ID: 24394873
This may be done without changing your layout but by inserting a simple javascript fuinction in your head block
<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=windows-1250">
  <title></title>
   <script type="text/javascript">
      window.onload = function() {
         var form = document.getElementById("form1");
         form.options.onchange = function(e) {
            form.notes.value = this.options[this.selectedIndex].text;
            return true;
         }
      }
   </script>
  
  </head>
  <body>
 
<form id="form1" action="" method="post">
        <select name="options">
                <option>Select a template</option>
                <option>Nobody at home</option>
                <option>Something else</option>
        </select>
        <textarea rows="15" cols="57" name="notes"></textarea><br />
        <input type="submit" value="">
</form>
 
  </body>
</html>

Open in new window

0
 
LVL 2

Author Comment

by:synergiq
ID: 24394960
Hi, both solutions work but is it possible to just add the text from the dropdown on to the end of what's in the textarea rather than replacing it?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Expert Comment

by:bugada
ID: 24394972
of course change line 9 as follows:



form.notes.value += this.options[this.selectedIndex].text;

Open in new window

0
 
LVL 10

Expert Comment

by:bugada
ID: 24394993
and try the code with IE, normally IE has strange effects...
0
 
LVL 2

Expert Comment

by:divstar
ID: 24395025
You may wish to add a new line so that it inserts underneath anything already present

In which case line 9 from bugada's code would be
form.notes.value += '\n'+this.options[this.selectedIndex].text;

Open in new window

0
 
LVL 2

Author Comment

by:synergiq
ID: 24395031
Perfect, thanks. I'll split the points as both solutions worked.
0
 
LVL 10

Expert Comment

by:bugada
ID: 24395059
Thank you, a good decision.
Happy that helped you.
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question