468,107 Members | 1,365 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,107 developers. It's quick & easy.

once again, how to add text to a textarea

How to add text to a textarea using javascript? Apparently this is a
popular question, because when I run the search on google, there are a
lot of returns:

http://groups.google.com/groups?hl=e...ang.javascript
However, all of the examples seem case specific, rather than generic.
I'm new to Javascript, so it is not so easy for me to take a specific
example and make it abstract enough to work as a generic function.

This example was given of how to add a newline to each line in a
textarea. I can imagine how to make this generic, but the example is
from 1999 and I'm wondering if this is still a valid way to go.

document.form1.textarea.value += document.form1.text.value + '\n';
Or, assuming I've a variable called myNewText, would this work better?
Would this work on all the major browsers?

document.getElementById(myTextarea).value .= myNewText;
I also saw this example from 2001. If I change "sendformreply" to the
name of my textarea (or is it the id of the textarea?) then could I
make this work for me?

function addto_text(smiley_txt) {
document.sendformreply.body.value += ' ' + smiley_txt + ' ';
document.sendformreply.body.focus();
}
Any help much appreciated. I'm sure I'll stop asking stupid questions
in about 2 months.
Jul 23 '05 #1
5 2759
lawrence wrote:
How to add text to a textarea using javascript? Apparently this is a
popular question, because when I run the search on google, there are a
lot of returns:

http://groups.google.com/groups?hl=e...ang.javascript
However, all of the examples seem case specific, rather than generic.
I'm new to Javascript, so it is not so easy for me to take a specific
example and make it abstract enough to work as a generic function.

This example was given of how to add a newline to each line in a
textarea. I can imagine how to make this generic, but the example is
from 1999 and I'm wondering if this is still a valid way to go.

document.form1.textarea.value += document.form1.text.value + '\n';
Did you test it?

Or, assuming I've a variable called myNewText, would this work better?
Would this work on all the major browsers?

document.getElementById(myTextarea).value .= myNewText;
Again, test it. But replace the PHP-type method of concatenation with +=

I also saw this example from 2001. If I change "sendformreply" to the
name of my textarea (or is it the id of the textarea?) then could I
make this work for me?

function addto_text(smiley_txt) {
document.sendformreply.body.value += ' ' + smiley_txt + ' ';
document.sendformreply.body.focus();
}


Again, test it :)

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #2
Lee
lawrence said:
This example was given of how to add a newline to each line in a
textarea. I can imagine how to make this generic, but the example is
from 1999 and I'm wondering if this is still a valid way to go.

document.form1.textarea.value += document.form1.text.value + '\n';
Yes. That's still the most reliable way to add text to the
end of a textarea, where "form1" is the NAME attribute of
the form, and "textarea" is the NAME attribute of the textarea.

Or, assuming I've a variable called myNewText, would this work better?
Would this work on all the major browsers?

document.getElementById(myTextarea).value .= myNewText;
You mean += not .=
That will work if "myTextarea" is the ID attribute (not the NAME)
of the textarea AND if the browser is modern enough to support
getElementById().

I also saw this example from 2001. If I change "sendformreply" to the
name of my textarea (or is it the id of the textarea?) then could I
make this work for me?

function addto_text(smiley_txt) {
document.sendformreply.body.value += ' ' + smiley_txt + ' ';
document.sendformreply.body.focus();
}


In that example, "sendformreply" is the name of the form,
and "body" is the name of the textarea.

Jul 23 '05 #3
Randy Webb <Hi************@aol.com> wrote in message news:<WI********************@comcast.com>...
lawrence wrote:
How to add text to a textarea using javascript? Apparently this is a
popular question, because when I run the search on google, there are a
lot of returns:

http://groups.google.com/groups?hl=e...ang.javascript
However, all of the examples seem case specific, rather than generic.
I'm new to Javascript, so it is not so easy for me to take a specific
example and make it abstract enough to work as a generic function.

This example was given of how to add a newline to each line in a
textarea. I can imagine how to make this generic, but the example is
from 1999 and I'm wondering if this is still a valid way to go.

document.form1.textarea.value += document.form1.text.value + '\n';


Did you test it?


Your answer could offer some details. What is the best way to test it?
How does one debug in Javascript?
Jul 23 '05 #4
lawrence wrote:
Randy Webb wrote:
Did you test it?


What is the best way to test it?
How does one debug in Javascript?


By using a browser with a script debugger. I recommend Mozilla Firefox.

http://www.mozilla.org/products/firefox/

--
Ben M.
Jul 23 '05 #5
Lee
lawrence said:

Randy Webb <Hi************@aol.com> wrote in message
news:<WI********************@comcast.com>...
lawrence wrote:
> How to add text to a textarea using javascript? Apparently this is a
> popular question, because when I run the search on google, there are a
> lot of returns:
>

http://groups.google.com/groups?hl=e...ang.javascript
>
>
> However, all of the examples seem case specific, rather than generic.
> I'm new to Javascript, so it is not so easy for me to take a specific
> example and make it abstract enough to work as a generic function.
>
> This example was given of how to add a newline to each line in a
> textarea. I can imagine how to make this generic, but the example is
> from 1999 and I'm wondering if this is still a valid way to go.
>
> document.form1.textarea.value += document.form1.text.value + '\n';


Did you test it?


Your answer could offer some details. What is the best way to test it?
How does one debug in Javascript?


You test it by creating a simple test case, as below.
I've also added a couple of alert()'s to show a simple
way to debug code:
<html>
<head>
<title>Testing</title>
<script type="text/javascript">
function appendText(field,str) {
alert("appending: \""+str+"\"");
field.value += str;
alert("new value is: \""+field.value+"\"");
}
</script>
</head>
<body>
<form name="form1">
<input name="newText">
<input type="button"
value="append text"
onclick="appendText(this.form.myArea,this.form.new Text.value)">
<input type="button"
value="newline"
onclick="appendText(this.form.myArea,'\n')">
<br>
<textarea name="myArea" cols="40" rows="10">Initial value.</textarea>
</form>
</body>
</html>

Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Doug van Vianen | last post: by
19 posts views Thread by dmiller23462 | last post: by
2 posts views Thread by Dung Ping | last post: by
2 posts views Thread by Rhys.Mataira | last post: by
5 posts views Thread by Chris | last post: by
13 posts views Thread by destiny007 | last post: by
1 post views Thread by Solo | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.