Mutasim Fuad
Mutasim Fuad

Reputation: 606

How to set ckeditor html data in a javascript variable

I am having a problem with ckeditor data. I have some ckeditor html data saved in my data base as a description. So when i need to update that description i need to set the data from the database and show it in the textarea in order to show user what was it. I'm using laravel 5.3. In order to do this i have tried this

var old_description = '<?php echo $Product->description;?>';
$('#detail').val(old_description);

But this is giving the following error

Uncaught SyntaxError: Invalid or unexpected token enter image description here

What is the problem and what should i do?

Upvotes: 0

Views: 444

Answers (2)

Ahmed Ginani
Ahmed Ginani

Reputation: 6650

You should just use json_encode() from http://www.php.net/manual/en/function.json-encode.php

var old_description = '<?php echo json_encode($Product->description); ?>';

It takes care of converting < / >, escaping any other special characters as necessary, preserve spaces, etc.

Upvotes: 1

ivop
ivop

Reputation: 38

The description you are passing contains multiple lines witch produces a javascript error. Try encoding the variable when echoing.

<?php echo json_encode($Product->description);?>

Upvotes: 1

Related Questions