Reputation:
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
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
Reputation: 2604
Try this. DEMO
$("#one").keyup(function(){
$("#two").val($(this).val())
});
I Hope it helps.
Upvotes: 1
Reputation: 722
If you want to do it in JS, do the following:
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
Reputation: 29683
You can use on
- input
too as below which responses for copy-paste too..
$("#one").on("input", function(){
$("#two").val($(this).val());
});
Upvotes: 1
Reputation: 8386
This is how I would do it:
$("#one, #two").on("change keyup", function(){
$("textarea").not($(this)).val($(this).val());
});
The code will synchronize both textareas
Upvotes: 4