472,805 Members | 1,320 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,805 software developers and data experts.

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 2941
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Doug van Vianen | last post by:
Hi, I am working on an Applet which provides some mouse practice for new computer users in our local seniors' computer club. The applet contains several cards, in a card layout, which are...
1
by: Volt | last post by:
is there any way to select and mark part of text in textarea by regular expression? i need to select the first string in textarea whitch is like xxxxx,xxx where x is any character
19
by: dmiller23462 | last post by:
Hi guys....I have absolutely NO IDEA what I'm doing with Javascript but my end result is I need two text boxes to stay hidden until a particular option is selected....I've cobbled together the...
1
by: IkBenHet | last post by:
Hello, I found this script to create a simple rich text form (http://programmabilities.com/xml/index.php?id=17): <html> <head> <title>Rich Text Editor</title> </head> <body>
2
by: Dung Ping | last post by:
I posted a similar question quite a while ago, but didn't get positive answer. Beofore turning to other approaches, I would like to check again. Following is what I like to do: Chinese is a...
2
by: Rhys.Mataira | last post by:
My code will not show up anything at all on the screen can someone guide me why this would be? <?php $pagetitle="Login"; If (!$_POST) or (!$_POST) { echo" <head></head> <style...
5
by: Chris | last post by:
I have a meetings section I'm developing on our intranet. Using PHP/MySQL. Meeting info and Meeting docs reside on 2 related tables in the db. Users may want to upload anywhere from 1 to 10 or...
13
by: destiny007 | last post by:
Hi, can any one help me to create the pop up only once. on clicking the text box <html> <head> <script type="text/javascript"> function abc() { var wind1=null;
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.