0

Here is my multiline editbox

<xp:inputTextarea id="taID"></xp:inputTextarea>

I want to count number of rows entered by user on e.g. button click. Rows are separated with new line. Not sure if I can use any kind of array because edit box may contain up to 200K rows. What is the best way to count it with JavaScript?

John Glabb
  • 1,336
  • 5
  • 22
  • 52
  • Possible duplicate of [How to count the number of lines of a string in javascript](http://stackoverflow.com/questions/8488729/how-to-count-the-number-of-lines-of-a-string-in-javascript) – Canolyb1 May 19 '17 at 17:19
  • I see.. but as I said .split would return 200,000 items.. I think it's not good solution – John Glabb May 19 '17 at 17:25

2 Answers2

0

Count the number of breaklines inside the content of the textarea. Be careful that it adds also 1 as the last one, so if you want to omit that you can subtract 1 to the total count:

function getBreakLines() {
  var content = document.querySelector("#txtarea").value;
  var numberOfLineBreaks = (content.match(/\n/g)||[]).length;
  alert(numberOfLineBreaks);
}
<textarea rows="10" cols="50" id="txtarea">
dsakjdjsal
dajsldkasjldkas
</textarea>
<br>
<button onclick="getBreakLines()">
get number of break lines
</button>

So in your case change the selector with #taID

quirimmo
  • 9,800
  • 3
  • 30
  • 45
0

You could extend the duplicates post with a non blocking function:

var somestring="abcdefgh\naghdfgakhsgfasgd";
var counted=0;
(function rec(i){
  for(a=Math.min(i+100,somestring.length);++i<a;){
  if(somestring[i]==="\n") counted++;
  }
  if(i===somestring.length) alert(counted) else setTimeout(rec,0,i+1);
})(0);
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151