开发者

Insert an \n instead of wrapping text in a textarea?

开发者 https://www.devze.com 2023-03-30 18:52 出处:网络
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.

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.


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.


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>


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消