Auto-expanding textarea

后端 未结 5 1929
执念已碎
执念已碎 2020-11-30 00:50

I\'m trying to do a simple auto-expanding textarea. This is my code:

textarea.onkeyup = function () {
  textarea.style.height = textarea.clientHeight + \'px\         


        
5条回答
  •  南方客
    南方客 (楼主)
    2020-11-30 01:09

    For those interested in a jQuery version of Rob W's solution:

    var textarea = jQuery('.textarea');
    textarea.on("input", function () {
        jQuery(this).css("height", ""); //reset the height
        jQuery(this).css("height", Math.min(jQuery(this).prop('scrollHeight'), 200) + "px");
    });
    

提交回复
热议问题