1

Is it possible to insert a newline (\n) whenever a textarea wraps in JavaScript? So basically, if there is an overflow, a newline separator should be inserted automatically.

And how would you do it? I need to insert explicit \n symbols. Just wrapping the text won't work.

Kristina
  • 15,859
  • 29
  • 111
  • 181
  • of course you meant \n not /n. I am not sure what you mean, because i think the default behaviour is that your text is wrapped to the new line.. – mkk Aug 23 '11 at 18:20
  • Yes, yes it is possible. – Jon Martin Aug 23 '11 at 18:21
  • 1
    Does http://stackoverflow.com/questions/4719777/finding-line-breaks-in-textarea-that-is-word-wrapping-arabic-text help? – Drazisil Aug 23 '11 at 18:28

3 Answers3

2

If you specifiy a wrap="hard" attribute on your textarea then a newline character will be sent in your form data wherever a visual break occurs in the textarea. The resulting tag would be:

<textarea name="mytext" wrap="hard"></textarea>
Joe Landsman
  • 2,177
  • 13
  • 9
2

If you are not submitting to a server then you need to splice the newlines in manually. Check out this Fiddle using jQuery. You could probably reduce the example down to straight JS easily enough if you don't want to use jQuery.

Joe Landsman
  • 2,177
  • 13
  • 9
0

I did it! I've created an iframe with a jsp inside and an identical textarea and before to send the textarea to the server by ajax, I made a submit to the jsp page, so then my data are correctly formatted. It works fine.