yuan eko
yuan eko

Reputation: 17

CKEDITOR does not work and error appears can not read property 'getEditor' at ckeditor.js:321

This is my JavaScript code:

var data = data.Data;
var html = "";      
for(var i = 0; i<data.length;i++){
     html = html +'<div class="form-group">'+
                       '<label class="control-label col-lg-2">Hasil Rapat</label>'+
                       '<div class="col-lg-10">'+
                           '<textarea rows="20" id="rapat" class="form-control rapat" name="hasil_rpt">'+data[i].hasil_rpt+'</textarea>'+
                       '</div>'+  
                   '</div>'
}

I have included the id on the textarea:

<textarea rows="20" id="rapat" class="form-control rapat" name="hasil_rpt">'+data[i].hasil_rpt+'</textarea>

And I also already use:

CKEDITOR.replace('rapat');

Upvotes: 0

Views: 1334

Answers (1)

mwilson
mwilson

Reputation: 12900

The first red flag I see is that CKEditor isn't going to know how to use your logic. I'm not sure which version of CKEditor you're using as things have drastically changed in v5. But, I think what you need to do is this:

var data = data.Data;
var html = "";      
for(var i = 0; i<data.length;i++) {
        var id = 'editorId_' + i;
     html = html +'<div class="form-group">'+
                       '<label class="control-label col-lg-2">Hasil Rapat</label>'+
                       '<div class="col-lg-10">'+
                           '<textarea rows="20" id="'+ id + '" class="form-control rapat" name="hasil_rpt">'+data[i].hasil_rpt+'</textarea>'+
                       '</div>'+  
                   '</div>';
    CKEDITOR.replace(id);
}

I'm not sure where you are calling CKEDITOR.replace from, but it should be either within the for loop or in a separate loop after you have completed the loop that adds your html to the DOM. The reason why your code isn't working is because you end up with multiple id's and id's are supposed to be unique. CKEDITOR is tripping over that more than likely.

EDIT

The above code isn't going to work since CKEDITOR needs your element reference to be present (and rendered) within the DOM. Once that element is ready, you can then call CKEDITOR.replace. A similar flow to this might work:

var data = data.Data;
var parentElement = ''; // <-- Get the reference to your container element here
for(var i = 0; i<data.length;i++) {
        var id = 'editorId_' + i;
     var html +'<div class="form-group">'+
                       '<label class="control-label col-lg-2">Hasil Rapat</label>'+
                       '<div class="col-lg-10">'+
                           '<textarea rows="20" id="'+ id + '" class="form-control rapat" name="hasil_rpt">'+data[i].hasil_rpt+'</textarea>'+
                       '</div>'+  
                   '</div>';
    parentElement.appendChild(html);
    CKEDITOR.replace(id);
}

Upvotes: 1

Related Questions