Textarea auto-resize in jquery

How can I make <textarea>auto-resizing with its contents (like on Facebook) using jQuery.

+2
source share
4 answers
    <script type="text/javascript">

    function adjustTextarea(this_){

    var value_ = this_.value;

    value_ = value_.replace(new RegExp("\n\r", 'gi'), "\n");
    value_ = value_.replace(new RegExp("\r", 'gi'), "\n");

    var split_ = value_.split("\n");
    this_.rows = split_.length;

    return;
    }

    </script>

    <textarea cols="100" style="overflow:hidden;" onkeyup="adjustTextarea(this);" onfocus="adjustTextarea(this);" >
    Text
    </textarea> 
+2
source

jquery http://james.padolsey.com/javascript/jquery-plugin-autoresize/

    $('textarea#comment').autoResize({
        // On resize:
        onResize : function() {
        $(this).css({opacity:0.8});
        },
        // After resize:
        animateCallback : function() {
        $(this).css({opacity:1});
        },
        // Quite slow animation:
        animateDuration : 300,
        // More extra space:
        extraSpace : 40
    });
0

:

- ? , JS, , - ... , -, : http://caniuse.com/#feat=contenteditable

, , ... .

, , .

http://jsfiddle.net/gbutiri/v31o8xfo/

<style>
.autoheight {
    min-height: 16px;
    font-size: 16px;
    margin: 0;
    padding: 10px;
    font-family: Arial;
    line-height: 16px;
    box-sizing: border-box;
    -moz-box-sizing: border-box;
    -webkit-box-sizing: border-box;
    overflow: hidden;
    resize: none;
    border: 1px solid #ccc;
    outline: none;
    width: 200px;
}
</style>
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script>
$(document).on('blur','.autoheight',function(e) {
    var $this = $(this);
    // The text is here. Do whatever you want with it.
    console.log($this.html());
});

</script>
<div class="autoheight contenteditable" contenteditable="true">Mickey <b>Mouse</b></div>
0

Source: https://habr.com/ru/post/1747971/


All Articles