PDA

View Full Version : From Text Field to Text Area Field


cebuy
06-08-03, 01:02 AM
I want some javascript code on getting a text field when filled out to input the same text into a textarea field. For instance, lets say that a person fills out the field for "city" in a form... I want that city name to be inserted into a textarea field like this... "I live in the city of ___" (whatever city they inputted into the form) Thanks!
Chris

Cagez
06-09-03, 05:58 PM
<html>
<head>
<title>Bleh</title>
</head>
</body>

<form name="myform">

<input type="text" name="name" onchange="document.myform.textareaname.value=this.value+' is the coolest'">
<br><br>
<textarea name="textareaname" cols="40" rows="5"></textarea>

</form>

</body>
</html>
Kinda like that?

cebuy
06-09-03, 10:13 PM
Yeah Exactly! Thanks!

cebuy
06-09-03, 10:18 PM
ONe more question... What if I want this to go down one line?
I tried, <input type="text" name="name" onchange="document.myform.textareaname.value=' <br>'+this.value+' is the coolest'" size="20"> but this just typed in the html. any ideas on this?

Cagez
06-09-03, 10:27 PM
You use the newline character:

<input type="text" name="name" onchange="document.myform.textareaname.value=this.value+' is the coolest\n'">


'\n' will insert a newline in the textarea. :)

amailer
06-09-03, 10:31 PM
oh i was just going to put that, but i gues you already posted it

Cagez
06-09-03, 10:41 PM
<html>
<head>
<title>Bleh</title>
<script language="javascript">
function add_it(obj, pre, suff)
{
texty = document.myform.mytextarea;
texty.value = texty.value+pre+obj.value+suff;
return true;
}
</script>
</head>
<body>

<form name="myform">
Name: <input type="text" name="name" onchange="add_it(this, '', ' is a cool name\n')"><br>
Job: <input type="text" name="job" onchange="add_it(this, 'Your job, a ', ' is such a neat job!\n')"><br><br>

<textarea name="mytextarea" cols="40" rows="5"></textarea>
</form>

</body>
</html>

You can also use that function to add multiple things to the box. The first argument is this (a pointer to the input box so we can use its value), the second argument is the prefix you want to add to the value, and the third is the suffix you wish to add :) Hope this helps.

amailer
06-09-03, 10:46 PM
Nice script Cagez, i like it :0

Cagez
06-09-03, 10:48 PM
Originally posted by amailer
Nice script Cagez, i like it :0
Thanks :o

cebuy
06-09-03, 11:55 PM
AWESOME! That \n is much like php (or is it perl? I remember that from somewhere ;-) Anyway, thanks for the cool code! Guess you can tell I am green but at least I am trying!

Be Blessed!
Chris

Cagez
06-09-03, 11:58 PM
\n is used all over the place ;) Though I think its \r (carritage return) on Macs though..