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
6 Answers
Reset to default 4This 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
版权声明:本文标题:javascript - How to copy from one textarea to another? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741218329a2360480.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论