PDA

View Full Version : Adding Text to Textarea From Dropdown


SteveSensei
02-15-2006, 04:07 PM
I need to add phrases contained in a select box to a textarea box with a comma between the phrases, like this:

"new, low mileage, air"

I have a script from another post that adds the words, but writes over the previous word:

<html>
<head>
<title>-</title>
<script type="text/JavaScript">
function showText(){
var F, opt;
F = document.oForm;
opt = F.oSel.options;

F.oTxt.value = opt[opt.selectedIndex].text;

}
</script>
</head>
<body>
<form name="oForm">
<select name="oSel" size="3"
onchange="showText()">
<option>lorem</option>
<option>ipsum</option>
<option>dolar</option>
</select>
<textarea name="oTxt" cols="10" rows="5">
</textarea>
</form>
</body>
</html>

I'm an ASP/VBScript programmer, but not a whiz on Javascript. Any help would be greatly appreciated.

arnyinc
02-15-2006, 04:19 PM
Just change:

F.oTxt.value = opt[opt.selectedIndex].text;

to

F.oTxt.value += opt[opt.selectedIndex].text;

SteveSensei
02-15-2006, 04:26 PM
Many thanks for your quick reply. Works like a charm, but I still need to add a comma after the word followed by a space:

For example, adding the words "new', "air" and "runs well" from the select box would result in "new, air, runs well" in the textarea box

Nischumacher
02-15-2006, 04:50 PM
TRY THiS...
function showText(){
var F, opt;
F = document.oForm;
opt = F.oSel.options;
if (F.oTxt.value!="")
F.oTxt.value += ", ";
F.oTxt.value += opt[opt.selectedIndex].text;
}

SteveSensei
02-15-2006, 05:04 PM
Perfect! Many many thanks for posting your replies so quickly and being spot-on with your answers.

Steve