actunderdc
actunderdc

Reputation: 1468

Textarea displays \n character instead of displaying endline properly

I am creating a project where I need to update a textarea field with info from mysql database.

If i manually put in the javascript:

document.getElementById('myTextArea').value='Line1\nLine2';

the textbox displays it properly.

In my script, at some point I update the value with a string received from an ajax request:

document.getElementById('myTextArea').value=result.slice(18);
console.log(result.slice(18));

In this case, the text area displays the text like this:

This should be the first line.\nThis should be the second line.\n.And so on...

At the console log I get properly the text:

This should be the first line.\nThis should be the second line.\n.And so on...

But I want it displayed in the textbox with endlines.

Below is a picture containig the sql table, the console output and the way textarea displays the text:

!http://s22.postimg.org/npzonvm9d/textarea.png

Thank you in advance.

Upvotes: 1

Views: 555

Answers (1)

ericdc
ericdc

Reputation: 11279

Answer was in comments.

The string is probably escaped, so the string is not really "\n", but "\n". Easiest (and ugliest) solution would be:

document.getElementById('myTextArea').value=result.slice.replace(/\\n/g, "\n");

I would suggest doing this server side

$result = str_replace('\\n', '\n', $stringfromdb);

Upvotes: 1

Related Questions