How to get string representation of an HTML5 textarea converting wraps into line breakings?

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?

Upvotes: 1

Views: 228

Answers (3)

Seems promising.Will test it here.

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

Thanks SO Related answers!

Upvotes: 0

GOTO 0
GOTO 0

Reputation: 47791

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

Upvotes: 1

user3222498
user3222498

Reputation:

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

Upvotes: 0

Related Questions