0

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
Community
  • 1
  • 1
TeaCupApp
  • 11,316
  • 18
  • 70
  • 150
  • what do you mean? When user presses enter you need to add a character? Which character? – Mihai Iorga Aug 31 '12 at 06:09
  • I think he wants the textarea to automatically add a LF as soon as the cursor gets wrapped to the next line after reaching the right side of the textarea. – Daniel M Aug 31 '12 at 06:12
  • @MihaiIorga, Sorry for not being very clear. But Daniel's explanation is perfect – TeaCupApp Aug 31 '12 at 06:14
  • Wouldn't that breaks the text ? – Mihai Iorga Aug 31 '12 at 06:18
  • @MihaiIorga: No, it wouldn't. The textarea displays a newline anyway. If you manage to add one at exactly this position where the textarea wraps the text, it won't make an optical difference. – Daniel M Aug 31 '12 at 06:26

1 Answers1

1

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.

Community
  • 1
  • 1
Daniel M
  • 3,369
  • 20
  • 30
  • My html is rendered thru Twig. I tried to check whether the height is being changed or not. But unfortunately the size of textarea remains constant. I guess only one option is left, calculate the width of char's and width of textarea and then add \n after every chunk. But its just very dirty solution – TeaCupApp Aug 31 '12 at 06:36