javascript - change textfield with every keyup - Stack Overflow

I'm trying to fill three fields with the same text but I'm only writing into the first one. T

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) to val(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
Add a ment  | 

4 Answers 4

Reset to default 4

See 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);    
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745411024a4626533.html

相关推荐

  • javascript - change textfield with every keyup - Stack Overflow

    I'm trying to fill three fields with the same text but I'm only writing into the first one. T

    11小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信