dk1990
dk1990

Reputation: 309

change textfield with every keyup

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

Upvotes: 2

Views: 1497

Answers (4)

Shushanth Pallegar
Shushanth Pallegar

Reputation: 2862

Text area value can be replaced with val()

  $('textarea[name="posting_twitter"]').val(text)

Upvotes: 2

JustAspMe
JustAspMe

Reputation: 418

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

Upvotes: 1

Josh Beam
Josh Beam

Reputation: 19802

See the working jsfiddle:

JS:

$('#first').on('keyup', function() {
   $('#second').val($(this).val()); 
});

HTML:

<input id="first" type="text">
<input id="second" type="text">

output

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.

Upvotes: 4

NightOwlPrgmr
NightOwlPrgmr

Reputation: 1374

Try this:

$( 'textarea[name="posting"]' ).on( "keyup", function() {

 var text = $('textarea[name="posting"]').val();
    $('textarea[name="posting_twitter"]').val(text);
      alert(text);

});

Upvotes: 2

Related Questions