how to count lines inside the textarea | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 13

how to count lines inside the textarea

Sample Code

2nd Sep 2020, 2:32 PM
RISHABH
RISHABH - avatar
7 Answers
+ 13
var count = 0; var a = document.getElementById("text") for (let i = 0; i < a.value.length; i++) { if (a[i] == "\n") { count++; } } console.log(count);
2nd Sep 2020, 3:01 PM
Namit Jain
Namit Jain - avatar
+ 5
assuming there are no text wrapping, count all of newline character in the value of the textarea. newline character is \n
2nd Sep 2020, 2:34 PM
Rei
Rei - avatar
+ 3
Sali65 that's a simple stuff Only the method is important 🤔
2nd Sep 2020, 4:01 PM
Namit Jain
Namit Jain - avatar
+ 2
Split it at every newline, then count the length of what the split returns.
3rd Sep 2020, 3:52 AM
Parsec
Parsec - avatar
0
Rei He means how can JS count it
2nd Sep 2020, 3:18 PM
Sali65
Sali65 - avatar
0
I think you have to make your own fxn for it. Fix the size of text area and once see how many characters lies in one line (say n). Store the length of string in textarea as textval Then Line_no = Math.ceil(textvalue.length/n); it will give line number in which you are writting I hope it will work as you want.
4th Sep 2020, 4:03 AM
Divya Mohan
Divya Mohan - avatar
0
Divya Mohan definitely possible if the font is monoscript and using ch unit as width but that's the only case, other than that it wont be as reliable
4th Sep 2020, 4:24 AM
Rei
Rei - avatar