Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resize input text field as text is entered

Is it possible to resize a text input box as text is typed? I've searched the web and haven't found anything.

like image 596
John Jacquay Avatar asked Dec 17 '22 11:12

John Jacquay


1 Answers

I just made one for you, try it here: http://jsfiddle.net/FNMSP/2/

function autoResize(e){
    var ele=e.target;                          //get the text field
    var t=ele.scrollTop;                       //use scroll top to determine if
    ele.scrollTop=0                              //space is enough
    if(t>0){                                       //If it needs more space....
        ele.style.height=(ele.offsetHeight+t+t)+"px";  //Then add space for it!
    }          
}

You can do this to the textarea,

<textarea onkeydown="autoResize(event)">Auto Resize!</textarea>

Or use below to attach the function to every <textarea>:

var ele=document.getElementsByTagName("textarea");
for(i=0;i<ele;i++){
    ele[i].addEventListener("keydown",autoResize,false)
}

Feel free to use it.

like image 52
Derek 朕會功夫 Avatar answered Dec 30 '22 06:12

Derek 朕會功夫