once again, how to add text to a textarea

L

lawrence

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=...a&btnG=Search&meta=group=comp.lang.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.
 
L

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.
 
R

Randy Webb

lawrence said:
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=...a&btnG=Search&meta=group=comp.lang.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 :)
 
L

Lee

lawrence said:
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.newText.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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top