15

I have a textarea. I need to update text in a div when a value in textarea is changed by either typing in it or by placing some value in it via another jQuery function or pasted.

$(document).ready(function(){
    function myFunc() {
        var input = $("#myTxt").val();
        $("#txtHere").text(input);
    }       
    myFunc();

    // EDIT BELOW -- this will update while typing
    $("#myTxt").keyup(myFunc);

});

<textarea id="myTxt"></textarea>
<div id="txtHere"></div>

It loads the value on page load but I'm not sure what to use to check for value in the textarea...

santa
  • 12,234
  • 49
  • 155
  • 255
  • Please don't edit the question to add the answer, this makes it confusing to know what is really the thing that was asked. – PhoneixS Sep 06 '22 at 15:10

7 Answers7

28
$(document).ready(function(){
    function myFunc(){
        var input = $("#myTxt").val();
        $("#txtHere").text(input);
    }       
    myFunc();

    //either this
    $('#myTxt').keyup(function(){
        $('#txtHere').html($(this).val());
    });

    //or this
    $('#myTxt').keyup(function(){
        myFunc();
    });

    //and this for good measure
    $('#myTxt').change(function(){
        myFunc(); //or direct assignment $('#txtHere').html($(this).val());
    });
});
2sh
  • 5
  • 3
Andri
  • 1,503
  • 13
  • 20
  • 1
    I think the last option is a good addition to update while typing. Can't I just do: $('#myTxt').keyUp(myFunc); ? – santa Aug 23 '11 at 18:23
  • 5
    "for good measure" sounds like you aren't really sure what's going to be executed – Druska Sep 19 '13 at 17:21
14

For textarea, the .change function only gets called when the textarea looses focus.

I found my solution at this link: How can I bind to the change event of a textarea in jQuery?

$('#textareaID').bind('input propertychange', function() {
      $("#yourBtnID").hide();

      if(this.value.length){
        $("#yourBtnID").show();
      }
});

This worked perfectly for me. It handles any type of change, including pasting.

Plus this.value.length is a neat way of seeing if the textarea has been cleared.

Cheers!

Community
  • 1
  • 1
Emma Li
  • 311
  • 2
  • 8
2
$(document).ready(function(){
   $('#myTxt').keypress(function(e){
       $('#txthere').html($(this).val());
   });
});

This will do that.

Senad Meškin
  • 13,597
  • 4
  • 37
  • 55
1

2018, without JQUERY

The question is with JQuery, it's just FYI.

JS

let myTxt = document.getElementById('myTxt');
let txtHere = document.getElementById('txtHere');
myTxt.addEventListener('input', function() {
    txtHere.innerHTML = myTxt.value;
});

HTML

<textarea id="myTxt"></textarea>
<div id="txtHere"></div>
rap-2-h
  • 30,204
  • 37
  • 167
  • 263
1

You can use delegate event like this:

$('body').delegate('#myTxt', 'keyup change', function(){
   $('#txtHere').text(this.value);
});

This should update the text as soon as you type in the text box or paste.

Sarfraz
  • 377,238
  • 77
  • 533
  • 578
  • I have another function where a value can be posted in this textarea by selecting from a list. But thinking of it, it's a textarea, pasting in is a possibility... – santa Aug 23 '11 at 18:21
0
$(document).delegate('#myTextareaId','input', function() {
  $('#myTextId').html($(this).val());
});
bycoder
  • 61
  • 1
  • 3
0

Use the change event handler (will activate when the content is changed and the textarea loses focus):

$('#myTxt').change(function(){
    var input = $("#myTxt").val();
    $("#txtHere").text(input );
});
JJ.
  • 5,425
  • 3
  • 26
  • 31