admin管理员组

文章数量:1279187

I have one page, with two textarea elements. How do I copy the text from one textarea to another?

<textarea id="one"></textarea>
<textarea id="two"></textarea>

So text area one is primarily where the data appears, I need to copy it to text area two during the onchange event.

I have one page, with two textarea elements. How do I copy the text from one textarea to another?

<textarea id="one"></textarea>
<textarea id="two"></textarea>

So text area one is primarily where the data appears, I need to copy it to text area two during the onchange event.

Share Improve this question edited Jan 7, 2016 at 13:00 Eric Galluzzo 3,2411 gold badge21 silver badges20 bronze badges asked Jan 7, 2016 at 12:48 user4596341user4596341 1
  • 5 Do $("#two").val($("#one").val()) onchange event – Parth Trivedi Commented Jan 7, 2016 at 12:51
Add a ment  | 

6 Answers 6

Reset to default 4

This is how I would do it:

$("#one, #two").on("change keyup", function(){
    $("textarea").not($(this)).val($(this).val());
});

Here is the JSFiddle demo

The code will synchronize both textareas

Try This.

function Copydata(){
  $("#two").val($("#one").val());
}
<script src="https://ajax.googleapis./ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<textarea id="one" onkeyup=Copydata();></textarea>
<br/>
<textarea id="two"></textarea>

You can use on - input too as below which responses for copy-paste too..

$("#one").on("input", function(){
    $("#two").val($(this).val());
});

DEMO

$('#one').on('keyup',function(){
    $('#two').val($(this).val());
});

JSFiddle Example

If you want to do it in JS, do the following:

Fiddle

function addEvent(el, name, func, bool) {
	if (el.addEventListener)
		el.addEventListener(name, func, bool);
	else if (el.attachEvent)
		el.attachEvent('on' + name, func);
	else el['on' + name] = func;
}

addEvent(one, 'keydown', function(e) {
	two.value = e.target.value;
}, false);
<textarea id="one"></textarea>
<textarea id="two"></textarea>

Try this. DEMO

$("#one").keyup(function(){
   $("#two").val($(this).val()) 
});

I Hope it helps.

本文标签: javascriptHow to copy from one textarea to anotherStack Overflow