简体   繁体   中英

how to insert value of an textarea to another textarea with javascript

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.

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 = "";

You could use jQuery:

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

to clear textArea you could use:

$("#first").text("");

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM