0

I am using ckeditor in my project which converts the user's input to html as follows:

<p>summary:</p>

<ul>
    <li>test</li>
    <li>test2</li>
    <li>test3</li>
</ul>

When fetching this data in jquery as follow:

var title = "{{$p->title}}";
var start = "{{$p->start_date}}";
var end = "{{$p->end_date}}";
var summary = '{{$p->summary}}';

I am getting the following error:

 Uncaught SyntaxError: Unexpected token ILLEGAL

at the summary var.By inspecting the element, it's as follow:

var summary = '<ul>
    <li>test</li>
    <li>tes2</li>
    <li>test3</li>
</ul>
';

I know it may sound a clumsy mistake but I can't figure out why it's not working.

omarsafwany
  • 3,695
  • 8
  • 44
  • 75
  • 1
    Read: [Creating multiline strings in JavaScript](http://stackoverflow.com/questions/805107/creating-multiline-strings-in-javascript) – David Sherret Jan 06 '15 at 17:50

1 Answers1

2

JavaScript string literals cannot include unescaped literal new line characters.

If you want to convert a PHP variables to a JavaScript string, then use json_encode, don't echo it out blindly.

$js_safe = array(
    title => json_encode($p->title),
    # etc
);

Then:

var title = {{$js_safe->title}};

Note that json_encode will add quotes for you.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335