By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,636 Members | 1,189 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,636 IT Pros & Developers. It's quick & easy.

Modify text in form based on selection (Option Select element)

P: 1
I'm a javascript newbie - so although I can follow scripts and write simple ones, I get stuck when things don't work.

I'm trying to have a help section on my web-site where each question/FAQ will be on a drop-down box (Option Select). Beneath this I've set a textarea and I wanted this to be populated with the answer based on the user selection.

I've created an array of my answers in the header area and tried to get an ONCHANGE on the select box to fill the textarea box based on the array and the selected index. Nothing works. If I hard-code the result it does i.e. ONCHANGE textarea="hardcoded value" - but not when I try ONCHANGE textarea = array[selectedindex value].

Am I missing something obvious - or how can I do this.

Thanks.
Sep 4 '06 #1
Share this Question
Share on Google+
1 Reply


acoder
Expert Mod 15k+
P: 16,027
If you have a textarea like this:
[html]<textarea id="textarea"></textarea>[/html]then call a function onchange:
[html]<select onchange="changeText(this.value);">
<option value="0">Test</option>
<option value="1">test 2</option>
<option value="2">test 3</option>
</select>[/html]and define the function and array like this:
Expand|Select|Wrap|Line Numbers
  1. var arr = ["Test 1","test 2","test 3\r\n"+"more"];
  2. function changeText(val) {
  3.     document.getElementById("textarea").value = arr[parseInt(val)];
  4. }
May 6 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.