I have certain piece of code which enables me to type in textarea. I am increasing width of textarea on keypress and i am giving width a limit after which it should stop increasing. What i want is on width limit reach stop typing and when user press enter key start typing again.
Heres Sample code :
var maindivwidth = $('.maindiv').width();
var vWidth = 0;
var hIncr = 2;
var tWidth = $('textarea')[0].value.length;
var iheight = $(dividfortextbox).css('font-size').replace('px','');
$(dividfortextbox).keypress(function(e) {
if ((e.keyCode || e.which) == 13) {
$(this).parent(dividfortext).css('height', (hIncr * iheight) + 'px');
vWidth = 0;
hIncr++;
}
else
{
vWidth = (vWidth+1);
if (tWidth < maindivwidth-aminus && vWidth*9 > tWidth){
tWidth = vWidth*9;
$(this).parent(dividfortext).css('width', (tWidth) + 'px');
}
else{
??????????????????????????
}
}
});
If I got you right, this may be what you’re looking for: Auto grow text area (jsFiddle)
No need for the user to press enter here to continue writing, the textarea automatically expands vertically. Depending on the nature of your project, this – although far from perfect! – might very well do the job. Please note that this method requires jQuery, although obviously it can be done without it.
HTML
CSS
Jquery