Is there anyway to have a textarea “autofit” height based on the content at page load?

前端 未结 13 1737
借酒劲吻你
借酒劲吻你 2020-12-15 17:46

Is there anyway through CSS or Javascript set the height of the textarea based on the content? I have a hardcoded height in my CSS but i wanted it to default so there is no

13条回答
  •  隐瞒了意图╮
    2020-12-15 18:16

    This work for me

    $(function(){
        $('body').on('keydown','textarea', function(event) {
            if ($('textarea')[0].clientHeight < $('textarea')[0].scrollHeight)
            {
                $('textarea').css({'height': $('textarea')[0].scrollHeight+"px"});
            }
            else
            {
                // reset height to default value
                $('textarea').css({'height': ''});
            }
        });
    });
    

提交回复
热议问题