Author Options:

How can I detect newlines in a html textbox? Answered

I have a big text box and an array called lines[]. I want each line of the text box to be stored in one of the array segments.
so if the text box was:

How Are

The lines variable world be:

lines[1] : Hello
lines[2] : How Are
lines[3] : You

I'm sorry is this is a simple question because i am new to coding.
I am using html and java-script.

Thanks, Griffin.



The forums are retiring in 2021 and are now closed for new topics and comments.
The Skinnerz
The Skinnerz

8 years ago

I don't know how similar java and C are, but in C I would store the whole input in an array, then have a loop go through this buffer one character an a time, copying the character to lines [n], where n is incremented each time the loop hits a newline character.


Answer 8 years ago

That's how I would do it also. For 'extreme' long strings it may help to limit the textbox maxlength but it should work,

on keypress, click, active, and losefocus you could call 'count lines'