1

I have a textarea HTML5 input with cols=120 set. When a user types a long string on it, the string may get wrapped into several lines, to fit the specified columns.

However, when I try to retrieve the text value of the element, the long string is not with any kind of line breaks associated.

Is there a way to get a string representation from the textarea which includes linebreaks exactly where the long string got wrapped, so that it goes the exact same way it was displayed into the screen?

double-beep
  • 5,031
  • 17
  • 33
  • 41

3 Answers3

1

You can use <textarea wrap="hard" ... to submit a form including line breaks as it appears to the user.

GOTO 0
  • 42,323
  • 22
  • 125
  • 158
  • I´ve tried this before asking, but didn´t work for my scenario. I´ve updated the question also. – Luiz Henrique Martins Lins Rol Mar 03 '14 at 06:18
  • @LuizHenriqueMartinsLinsRol Are you trying to the get the text in a script? If so, I'd suggest you add a tag `javascript` to your question. An example of what you're expecting and what you're getting would also help. – GOTO 0 Mar 03 '14 at 06:24
  • I see your point @GOTO0. I´m trying to fetch the value on a angularjs script indeed, and pass it to a XHR request later on. Would it work if it wasn´t an Ajax call, but a plain form-submit? – Luiz Henrique Martins Lins Rol Mar 03 '14 at 19:42
0

Use .val() to get value of textarea and use $.trim() to empty spaces.

$(document).ready(function () {
var val = $.trim($("textarea").val());
if (val != "") {1
    $('div').append('<pre>'+val+'</pre>');
}
});

Demo : jsfiddle

0

Seems promising.Will test it here.

finding "line-breaks" in textarea that is word-wrapping ARABIC text

Thanks SO Related answers!

Community
  • 1
  • 1