1

I'm trying out the CKeditor in laravel and I encountered an issue inserting html into the editor. After appending the editor I'd like to set the value so you can edit the already existing values.

HTML:

@if (!empty($proposal->reference_sites))
    <div class="form-group row mb-4">
        <label for="reference_sites" class="col-sm-3 col-form-label form-control-lg">Reference sites:</label>
        <div class="col-sm-10" id="reference_sites"></div>
    </div>
@endif

JS:

if($('#reference_sites').length){
    $output = "<textarea id='ckeditor-rs' name='ckeditor-rs' rows='10' cols='80'></textarea>";
    $('#reference_sites').append($output);
    CKEDITOR.replace('ckeditor-rs');

    var editor = CKEDITOR.instances['ckeditor-rs'];
    editor.setData("{!!html_entity_decode($proposal->reference_sites)!!}");

    }else{
        console.log("couldn't append ckeditor in rs");
    }

As you see I'm trying to decode the HTML and set that as the HTML of the CKeditor.

$proposal->reference_sites contains this HTML:

<ul>
    <li>site one</li>
    <li>site two</li>
    <li>site 3</li>
</ul>

Error: Uncaught SyntaxError: Invalid or unexpected token <

I'm not entirely sure what causes this error since when I only decode a variable which contains <p>some text</p> it inserts some text into the editor.

Any help is greatly appreciated!

4
  • try to do it without html_entity_decode Commented Apr 2, 2019 at 11:04
  • returns the same error but instead of html i get &lt;ul&gt; &lt;li&gt;site one&lt;/li&gt; &lt;li&gt;site two&lt;/li&gt; &lt;li&gt;site 3&lt;/li&gt; &lt;/ul&gt; Commented Apr 2, 2019 at 11:06
  • editor.setData("{$proposal->reference_sites}"); Commented Apr 2, 2019 at 11:08
  • 1
    Alright after 3 hours I finally found out what the problem was. In the database you have the "enter" entities too and they messed up the javascript as it would litterally put an enter in there. Commented Apr 2, 2019 at 12:17

2 Answers 2

1

try the following one,

if($('#reference_sites').length){
$output = "<textarea id='ckeditor-rs' name='ckeditor-rs' rows='10' cols='80'></textarea>";
$('#reference_sites').append($output);
CKEDITOR.replace('ckeditor-rs');

var editor = CKEDITOR.instances['ckeditor-rs'];
editor.setData("{{$proposal->reference_sites}}");

}else{
    console.log("couldn't append ckeditor in rs");
}
Sign up to request clarification or add additional context in comments.

5 Comments

this inserts {$proposal->reference_sites} as html. Which is the variable name and not the value of the variable.
i assume $proposal->reference_sites contains html,you can try to echo as html string editor.setData("<?php echo $proposal->reference_sites ?>"); if its not,then you are make some syntax error
Like I said in the original question the error is: Uncaught SyntaxError: Invalid or unexpected token < line 254
0

You can try this one code:

if($('#reference_sites').length){
    $output = "<textarea id='ckeditor-rs' name='ckeditor-rs' rows='10' cols='80'></textarea>";
    $('#reference_sites').append($output);
    CKEDITOR.replace('ckeditor-rs');

    for (var i in CKEDITOR.instances) {
        CKEDITOR.instances[i].on('change', function() { 
            CKEDITOR.instances[i].updateElement() 
        });
    }
}else{
    console.log("couldn't append ckeditor in rs");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.