最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

how to insert value of an textarea to another textarea with javascript - Stack Overflow

matteradmin8PV0评论

Well this is what i mean. Lets say i have 2 textarea:

<textarea id="first" class="txtarea" name="in_first" cols="80" rows="15">First Textarea</textarea>

and:

<textarea id="second" class="txtarea" name="in_second" cols="80" rows="15">First Textarea</textarea>

In the final result, I want to move the value of the first textarea to the second textarea with javascript only, please do not sugest me any other programing language.

I already get the value of the first textarea with code like the following:

var textAreaValue = $("#first").text();

now, how can i insert it to the second textarea? or maybe you have another method, please let me know.

Well this is what i mean. Lets say i have 2 textarea:

<textarea id="first" class="txtarea" name="in_first" cols="80" rows="15">First Textarea</textarea>

and:

<textarea id="second" class="txtarea" name="in_second" cols="80" rows="15">First Textarea</textarea>

In the final result, I want to move the value of the first textarea to the second textarea with javascript only, please do not sugest me any other programing language.

I already get the value of the first textarea with code like the following:

var textAreaValue = $("#first").text();

now, how can i insert it to the second textarea? or maybe you have another method, please let me know.

Share Improve this question asked May 20, 2013 at 9:53 RK26RK26 3831 gold badge7 silver badges20 bronze badges 2
  • 1 Uhm, but you're using jQuery ? – adeneo Commented May 20, 2013 at 9:55
  • 1 We nay guess that under any other programming language you mean something like VBScript. If you mean jQuery, then jQuery is a JavaScript library. So what exactly do you mean by any other programming language? – VisioN Commented May 20, 2013 at 9:58
Add a ment  | 

6 Answers 6

Reset to default 6

Using jquery val() method:

$firsttextarea=$("#first").val();
$('#second').val($firsttextarea);

Using jquery text() method:

$firsttextarea=$("#first").text();
$('#second').text($firsttextarea);

Then, with JS only (no libraries):

var firstTextArea = document.getElementById('first');
document.getElementById('second').value = first.value;

//clearing the first
first.value = '';

In pure JavaScript it should look like:

var value = document.getElementById("first").value;
document.getElementById("second").value = value;

Pay attention that for form elements (like <textarea>) you should address value property.

If you want to clear the value of the first <textarea> then do:

document.getElementById("first").value = "";

With jQuery

$('.second').val($('.first').val());

This is just for your information.

html

<textarea id="first" class="txtarea" name="in_first" cols="80" rows="15">First Textarea</textarea>

<textarea id="second" class="txtarea" name="in_second" cols="80" rows="15">First Textarea</textarea>

jquery

var FristTextAreaValue = $("#first").val();
var SecondTextAreaValue = $("#second").val();

You could use jQuery:

var textAreaValue = $("#first").text();
$("#second").text(textAreaValue);

to clear textArea you could use:

$("#first").text("");
Post a comment

comment list (0)

  1. No comments so far