Line breaks not showing up in textarea in IE

Using the jQuery .load() method, I load text into a text field. Works great in Chrome and FF. As always, IE just needs to be different and will not display line breaks.

I tried white-space:pre-wrap with no luck.

Any ideas?

My code is:

 $('#textarea').load('data.php'); 

data.php simply queries the MySql database and prints the results.

+6
source share
3 answers

These are IE compatibility issues when using innerHTML() . Since the JQuery .html() and .load() methods use innerHTML() , they can lead to some problems by extension. One solution is to use .text() . If you want to load text into <textarea> using AJAX and JQuery, you need to do something like this:

 $('#textarea').post('data.php',function(data){ $(this).text(data); }) ); 
+4
source

This is probably a line ending problem . Line break in text \n or \r\n s? If they are just \n , try normalizing the text. You can do this using JavaScript:

 function normalizeNewlines(text) { return text.replace(/(\r\n|\r|\n)/g, '\r\n'); } 

This is a solution that worked for me in the past when it did essentially the opposite: I needed to extract text from <pre> , which can be inserted into !@ #$ing Notepad and displayed with line breaks.

+8
source

I used the kendoui grid in the built-in editing mode, a solution was used to describe the text description field, for example, the built-in style:

 <textarea style="white-space:pre-wrap;" name="' + options.field + '" ... 

And the following code in the keydown event:

 if (event.keyCode == 13) { event.stopPropagation(); } 
+1
source

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


All Articles