Dynamically expand height of input type “text” based on number of characters typed into field

后端 未结 3 1239
遇见更好的自我
遇见更好的自我 2020-12-01 10:26

Similar to the below JSFiddle (which I bookmarked and do not know from where the original question emerged):

http://jsfiddle.net/mJMpw/6/



        
3条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-01 10:47

    Not mine but works great:

    CSS

    textarea {
         color: #444;
         padding: 5px;
    }
    .txtstuff {
        resize: none;
        overflow: hidden;
    }
    .hiddendiv {
        display: none;
        white-space: pre-wrap;
        word-wrap: break-word;
        overflow-wrap: break-word
    }
    .common {
        width: 500px;
        min-height: 50px;
        font-family: Arial, sans-serif;
        font-size: 13px;
        overflow: hidden;
    }
    .lbr {
        line-height: 3px;
    }
    

    HTML

    
    

    SCRIPT

    let txt = $('#comments'),
        txt.addClass('txtstuff');
    let hiddenDiv = $(document.createElement('div')),
        hiddenDiv.addClass('hiddendiv common');
    let content = null;
    
    $('body').append(hiddenDiv);
    
    txt.on('keyup', function () {
        content = $(this).val();
        content = content.replace(/\n/g, '
    '); hiddenDiv.html(content + '
    '); $(this).css('height', hiddenDiv.height()); });

    FIDDLE

提交回复
热议问题