Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update text on textarea value change w/ jQuery

Tags:

jquery

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...

like image 239
santa Avatar asked Aug 23 '11 18:08

santa


People also ask

How to change value of textarea in jQuery?

You can simply use the val() method to set the value of a textarea dynamically using jQuery.

How do you change textarea text?

Alternatively, you can use jQuery's . html() method, which uses the browser's innerHTML property to replace textarea content with the new content completely. Another good solution is to use the . val() method to set the text value of the textarea element.

Can we use value attribute in textarea?

<textarea> does not support the value attribute.

What is value of textarea?

The value property sets or returns the contents of a text area. Note: The value of a text area is the text between the <textarea> and </textarea> tags.


1 Answers

$(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());
    });
});
like image 142
Andri Avatar answered Sep 22 '22 01:09

Andri