Log in

View Full Version : Generate Text



kaos
08-09-2009, 11:45 PM
All right, Could use some help here... I need to make a script that will alow me to generate text inside of a <textarea>. For example:



<select id="select">
<option>option 1</option>
<option>option 2</option>
</select>
<br><br><br>
<input type="button" value="Generate"><br>
<textarea cols=10 rows=5></textarea>



When you click 'Generate', text specified by the value of selct menu is placed within the <textarea>. Anyone have an Idea???

vwphillips
08-10-2009, 08:04 AM
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title></title>
<script type="text/javascript">
/*<![CDATA[*/

function Text(from,to){
if (document.getElementById(from).value) document.getElementById(to).value=document.getElementById(from).value;
}
/*]]>*/
</script></head>

<body>
<select id="select">
<option value="Text 1" >option 1</option>
<option value="Text 2">option 2</option>
</select>
<br><br><br>
<input type="button" value="Generate" onclick="Text('select','ta')"><br>
<textarea id="ta" cols=10 rows=5></textarea>
</body>

</html>

or


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title></title>
<script type="text/javascript">
/*<![CDATA[*/
var Ary=[];
Ary[0]='Text One';
Ary[1]='Text Two';

function Text(from,to,ary){
var index=document.getElementById(from).selectedIndex;
if (ary[index]) document.getElementById(to).value=ary[index];
}
/*]]>*/
</script></head>

<body>
<select id="select">
<option>option 1</option>
<option>option 2</option>
</select>
<br><br><br>
<input type="button" value="Generate" onclick="Text('select','ta',Ary)"><br>
<textarea id="ta" cols=10 rows=5></textarea>
</body>

</html>

kaos
08-10-2009, 05:05 PM
Thanks, but is it possible to hold the select option's values that will be placed in the textarea in an external file(s)?

vwphillips
08-11-2009, 09:26 AM
simplest would be to define an array in an external javascript file, this could also be used to define the select list options.