I am trying to have a simple Rich Text editor, I am trying to achieve that using Summernote. JQuery and Bootstrap are both imported. When i use the Summernote RTE in HTML it works perfectly. But now i need to create many dynamic RTEs using Javascript. I create the RTE using imperative JS and it shows up, i then initialize it and i looks like it should. It also saves like it should. But whenever you would try to lets say bold, or italic or change font color, it sends your cursor to the start of the line. If you then start typing it works, but i need to be able to highlight text and then apply the modifications.
This is my JS function for creating the RTE (it is later appended to the document) code:
function createRTE(lang, field_name = 'description', required = true) {
const rte_div = document.createElement('div');
const rte = document.createElement('div');
rte.setAttribute('name', 'content[' + global_counter + '][' + field_name +']['+lang+']');
rte.setAttribute('class', 'summernote');
rte.setAttribute('data-textarea', 'rte_' + global_counter + '_' + lang);
const rte_textarea = document.createElement('textarea');
rte_textarea.setAttribute('name', 'content[' + global_counter + '][' + field_name +']['+lang+']');
rte_textarea.setAttribute('id', 'rte_' + global_counter + '_' + lang);
rte_textarea.setAttribute('hidden', 'true');
rte_div.appendChild(rte_textarea);
rte_div.appendChild(rte);
return rte_div;
}
And this is my initialization:
function initSummernote(selector = '.summernote') {
if (!$.fn.summernote) {
return;
}
const elems = document.querySelectorAll(selector);
for (const elem of elems) {
$(elem).summernote({
dialogsFade: true,
dialogsInBody: true,
height: elem.dataset.height ?? 300,
fontNames: [],
maximumImageFileSize: 1024 * 1024,
tableClassName: 'table table-hover table-bordered',
toolbar: [
['actions', ['undo', 'redo']],
['style', ['style']],
['font', ['bold', 'italic', 'underline', 'strikethrough', 'superscript', 'subscript', 'clear']],
['color', ['color']],
['para', ['ul', 'ol', 'paragraph', 'hr']],
['table', ['table']],
['insert', ['link', 'picture', 'video']],
['view', ['fullscreen', 'codeview', 'help']]
],
callbacks: {
onChange(contents) {
if (elem.dataset.textarea) {
document.getElementById(elem.dataset.textarea).value = $(elem).summernote('isEmpty') ? '' : contents.trim();
}
}
}
});
}
}

