티스토리 뷰

Javascript

textarea 동적으로 resizing 하기

진태우 2018. 7. 20. 11:21

// html code

<textarea data-autoresize rows="2"></textarea>


// javascript code
jQuery.each(jQuery('textarea[data-autoresize]'), function() {
    var offset = this.offsetHeight - this.clientHeight;
 
    var resizeTextarea = function(el) {
        jQuery(el).css('height', 'auto').css('height', el.scrollHeight + offset);
    };
    jQuery(this).on('keyup input', function() { resizeTextarea(this); }).removeAttr('data-autoresize');
});


// css code
textarea {
    box-sizing: border-box;
    resize: none;
}


리사이징이 너무 잘되도록 어떤 외쿡 형님이 작성해놔서 숟가락만 얹었다....


퍼옴 - 외쿡형님 블로그

'Javascript' 카테고리의 다른 글

간단하게 날짜 포맷 변경해보기  (0) 2018.06.20
history.pushState 사용하기  (0) 2017.07.07
cookie 핸들링 해보기  (0) 2017.01.24
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함