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
  • 11,716
  • 43
  • 149
  • 239

7 Answers7

27
$(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,473
  • 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
13

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
  • 301
  • 2
  • 8
2
$(document).ready(function(){
   $('#myTxt').keypress(function(e){
       $('#txthere').html($(this).val());
   });
});

This will do that.

Senad Meškin
  • 13,327
  • 4
  • 40
  • 54
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
  • 26,857
  • 31
  • 150
  • 246
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
  • 367,681
  • 72
  • 526
  • 573
  • 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
  • 51
  • 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,315
  • 3
  • 25
  • 31