Thanks for the people who answered here.
I totally forgot about this one, well I've already finished it last week.
Here's what I've come up with.
Note: This is not all of my code, I just chopped that includes on this problem.
I created a counter for it.
var ctr = 0;
//Editor Name counter
var editor = "e_" + ctr;
// Do create text editor
$("#editor-container").prepend("<textarea id='"+ editor +"' name='"+ editor +"' data-num='"+ ctr +"' class='text-editor' rows='10' cols='80'></textarea>");
// Do re-create CKeditor instance
if (CKEDITOR.instances[editor]) {
delete CKEDITOR.instances[editor];
} CKEDITOR.replace(editor);
// Do check existing textarea
if (ctr > 0) {
// Do loop textarea
$(".text-editor").each( function() {
var text_editor = $(this);
var text_editor_id = text_editor.attr("id");
ctr_editor = parseInt(text_editor.attr("data-num")) - 1;
// Do remove recent textarea
if (ctr_editor < ctr && ctr_editor >= 0) {
$("#e_" + ctr_editor + ", #cke_e_" + ctr_editor ).remove();
}
});
}
// Do get contents of div
var default_val = contents.find(".editable[name='"+ cb +"']").html();
CKEDITOR.instances[editor].setData(default_val);
CKEDITOR.instances[editor].on("change", function() {
var value = CKEDITOR.instances[editor].getData();
contents.find(".editable[name='"+ cb +"']").html(value);
})
// Do increment counter
ctr++;
So when the user saves the editor using save button
/* Close editor */
$("#save-editor").on("click", function() {
$("#editor-wrapper").animate({ width: 0 });
// Do update editor name counter
editor = "e_" + ctr;
cb = "cb" + ctr;
});