2

I have text containing line breaks from a TextArea stored in a database. A php script tries to retrieve this text and store it in a JavaScript variable. The problem is that the string is not properly understood by JavaScript and it is interpreted that JavaScript variable is not correctly ending because JavaScript assumes a line break.

Any idea how could I save text containing line breaks in a JavaScript variable?

Thanks in advance.

john518
  • 359
  • 1
  • 4
  • 12

1 Answers1

1

Any idea how could I save text containing line breaks in a JavaScript variable?

Yes, Use PHP's json_encode:

 var myvar = <?php echo json_encode($myVarValue); ?>;
Yair Nevet
  • 12,725
  • 14
  • 66
  • 108