Increase textarea height with text

Can textarea adapt its height when inserting text?

I want to hide the borders of the textarea so that users feel like they are gaining unlimited space.

(The height of the text area should be increased one line height at the start of a new line)

One way I guessed it is; copy all texts in div with the same width on every key press, then measure the height of the div, then set the height of the div for the textbox.

One issue I have noticed is the scrollbar width, which has to be subtracted from the main width, and on different devices it is 0-16 pixels of variable scrollbar width ..., any suggestions?

+3


source to share


2 answers


You can easily achieve what you are trying to do using elastic.js , this is a simple one-off version, linear solution.



$('#note').elastic();
      

textarea#note {
	width:100%;
	display:block;
	resize: none;
	border: none;
}
textarea:focus {
	outline: none;
}
      

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://jquery-elastic.googlecode.com/svn-history/r37/trunk/jquery.elastic.js"></script>
<textarea id="note">Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.</textarea>
      

Run codeHide result


0


source


This is what you want First: html is a text area with id: ta p>



<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <textarea rows="10" cols="10" id="ta">
    </textarea>
      

Run codeHide result


And here is jquery code that works like this: which increases the height (number of lines) of the text area every time you hit the last line



  $('#ta').keypress( function(){
                    var rows=$('#ta').attr('rows');
                    var text=$('#ta').val();
                    var lines=text.split('\n');
                    if(lines.length==rows){
                        rows++;
                        $('#ta').attr('rows',rows);
                    }
                });
      

Run codeHide result


And this css is only for hiding the border of the textbox so that the user feels like he / she is writing on the page



#ta{
      border: 0;
      }
      

Run codeHide result


0


source







All Articles