4

Is there any way to detect in jQuery; whether new row got created in text area while end user is typing continuously in it? I could not set line height, max height of minheight of textarea.

enter image description here

Fiddle: http://jsfiddle.net/5wdzH/113/

Note: Here i am not asking about \n (Enter) but about new Row

   enteredText = $( this ).val();
   numberOfLineBreaks = (enteredText.match(/\n/g)||[]).length; //When we press enter then only the value comes but not for new row.

Textarea attributes:

  $(e).css({'height': 'auto', 'overflow-y': 'hidden'}).height(e.scrollHeight);
fatherazrael
  • 5,511
  • 16
  • 71
  • 155

2 Answers2

1

I think best way to measure it will be counting number of columns of the text area and check if the number of characters entered exceeds that or not.This will not possibly hampered by wide letters or slim ones (except for one or two characters error margin).

var numberOfColumns = 0; var numberOfLines = 1;

First:

$( "#watched_textarea" ).each(function(){
        numberOfColumns = $(this).context.cols;
});

And then:

if (characterCount > numberOfColumns) {
    numberOfLines = parseInt(characterCount / numberOfColumns) + 1;
} 

You can have a look - updated your fiddle: http://jsfiddle.net/5wdzH/116/

Himel Nag Rana
  • 744
  • 1
  • 11
  • 19
0

There are two ways to do that 1) if you want to identify enter press then you can increase row counter if(e.keyCode == 13) { //increment row counter }

2) if you are using fixed width of textarea then you can count number of words can fit in one row. no of character divide this factor to will give you number of rows

i think this will help you

  • Number 2 will fail badly - check the width of 11111 versus 88888 - both 5 characters but 8s are much wider than 1s – StudioTime Apr 19 '16 at 07:27