I have textarea, user keeps writing inside it without pressing enter. If they press enter I get \n char which I need, but sadly they are not pressing enter and instead they just keep typing!
Now, Is there any way I can add new line character inside the string just after the textarea move to next line?
Potential Duplicate : Automatic newline in textarea
Note : The question shared above has an accepted answer. But the link is not working 🙁 The other options aren’t very efficient.
I have several technologies to manipulate the String I am getting from <textarea>. So feel free to give solution for any single one.
- HTML
- JavaScript
- Twig
- php
- CSS
One way to solve this problem is to make the text area auto-resizing. Thus, the user starts with a single line textarea and as soon as the text becomes too long, the textarea adjusts its height to give a new empty line to the user.
Check this question or do some research using your fav. search engine if you need a way to do this.
As soon as the script triggers a height change, you can append a newline before the latest character.
// Update: You might want to check the behavior of a textarea because the text gets most likely wrapped before the latest word. You’ll have to write an algo which retrieves the position in the text where you need to insert the LF in order not to break the text. This sounds complicated but shouldn’t be too hard. Just check after which chars the text gets wrapped and search for the last appearance of one of these chars.