Textarea in IE8 Newline Problem

I am making a cross-browser form that requires a text box. This text box receives data in two ways: - Send an Ajax call to the server and return data - User data

http://jsfiddle.net/garrettwong/x3KSP/

I have a problem with IE8 where the textarea text is not formatted (new lines cannot be read), where, like in Chrome, the same code formats the text field well. I hope to use only a JavaScript solution if possible, but I'm not sure where to go from here.

+4
source share
3 answers

Why are you using text() ? Set the value using val() .

Newlines are usually \n\r .

+8
source

Perform a replace operation to verify that \r is before \n :

 str = str.replace(/\r?\n/g, "\r\n"); 

Fiddle

Browsers use \r\n for line breaks, although modern browsers such as Chrome and Firefox can also handle \n Unix-style line breaks. IE8 requires proper line breaks \r\n , which also work in all other browsers.

And as @epascarello pointed out, use .val() to control the textarea value property.

+6
source

Just use "\ r \ n" for newlines.

+3
source

Source: https://habr.com/ru/post/1416073/


All Articles