user4596341
user4596341

Reputation:

How to copy from one textarea to another?

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.

Upvotes: 1

Views: 6854

Answers (6)

Ankit Kathiriya
Ankit Kathiriya

Reputation: 1231

Try This.

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

Upvotes: 3

Sapikelio
Sapikelio

Reputation: 2604

Try this. DEMO

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

I Hope it helps.

Upvotes: 1

PDKnight
PDKnight

Reputation: 722

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>

Upvotes: 1

Baro
Baro

Reputation: 5530

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

JSFiddle Example

Upvotes: 1

Guruprasad J Rao
Guruprasad J Rao

Reputation: 29683

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

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

DEMO

Upvotes: 1

Ahs N
Ahs N

Reputation: 8386

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

Upvotes: 4

Related Questions