admin管理员组文章数量:1426486
I'm trying to fill three fields with the same text but I'm only writing into the first one. This code is only working once and then it isn't (the alert is working fine constantly).
$( 'textarea[name="posting"]' ).on( "keyup", function() {
var text = $('textarea[name="posting"]').val();
$('textarea[name="posting_twitter"]').replaceWith(text);
alert(text)
});
I'm trying to fill three fields with the same text but I'm only writing into the first one. This code is only working once and then it isn't (the alert is working fine constantly).
$( 'textarea[name="posting"]' ).on( "keyup", function() {
var text = $('textarea[name="posting"]').val();
$('textarea[name="posting_twitter"]').replaceWith(text);
alert(text)
});
Share
Improve this question
edited Jun 22, 2015 at 15:16
j08691
208k32 gold badges269 silver badges280 bronze badges
asked Jun 22, 2015 at 15:12
dk1990dk1990
3093 silver badges15 bronze badges
4
-
3
Have you tried changing
replaceWith(text)
toval(text)
? jsfiddle/j08691/ccp7eopf/1 – j08691 Commented Jun 22, 2015 at 15:15 - 1 @j08691 - you are right – Sudharsan S Commented Jun 22, 2015 at 15:16
- try adding event.preventDefault(); – Timotheus0106 Commented Jun 22, 2015 at 15:16
- 1 j08691 is correct. When you use replaceWith(text) it is actually replacing the selected element(s) with that string rather than updating the value. – mason81 Commented Jun 22, 2015 at 15:19
4 Answers
Reset to default 4See the working jsfiddle:
JS:
$('#first').on('keyup', function() {
$('#second').val($(this).val());
});
HTML:
<input id="first" type="text">
<input id="second" type="text">
All you need to do is use jQuery's .val()
method, which can both set and get the value of an input
element. Read the documentation.
Try this:
$( 'textarea[name="posting"]' ).on( "keyup", function() {
var text = $('textarea[name="posting"]').val();
$('textarea[name="posting_twitter"]').val(text);
alert(text);
});
Text area value can be replaced with val()
$('textarea[name="posting_twitter"]').val(text)
like this?
html
<textarea name = "posting"></textarea>
<textarea name = "posting_twitter"></textarea>
jquery
$('textarea[name="posting"]').on("keyup", function(){
var text = $(this).val();
$('textarea[name="posting_twitter"]').val(text);
});
本文标签: javascriptchange textfield with every keyupStack Overflow
版权声明:本文标题:javascript - change textfield with every keyup - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745411023a2657469.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论