Hello, I am trying to load SunEditor to multiple
All my
Uncaught Error: [SUNEDITOR.create.fail] The ID of the suneditor you are trying to create already exists (ID:"suneditor")
at Object.create (suneditor.min.js:1)
at Object.
at (index):3783
Is there an established workaround for that?
Many thanks,
@ATOPBIO ID attributes must be unique.
<textarea id="id1"/>
<textarea id="id2"/>
const instance1 = suneditor.create("id1")
const instance2 = suneditor.create("id2") // or suneditor.create(document.getElementById("id2"))
That is not workable when you have text areas generated, on the page, for example I create new post, and each post allows editing, so each edit box needs an instance.
I would suggest that the editor automatically gets assigned to all instances of the same ID maybe at the next update.
Thank you for your response and nevertheless, Suneditor is a great and robust editor, well done.
Perhaps we can use something alternatiing to document.getElementsByClassName()
To assign to one class with multiple instances rather than IDs?
@ATOPBIO One instance per textarea.
To create multiple editor instances within a page,
you need to define multiple textarea and create editor instances for each textarea.
Am I misunderstanding the question?
I am using SunEditor in a comments box, with a 'reply' feature, means every textarea needs a new script.
I am trying to write one script and render on all textareas within the same page.
After doing some research I came to realise applying JS to a CLASS will apply to all classes, something alternating to
document.getElementsByClassName()
I am not able to get that to work yet. Will keep trying.
Since individual instances are created per editor, "create" must be called multiple times.
Even if you take the "textarea" as an array, only the last instance of the editor created will remain.
EX)
const reply = document.getElementsByClassName("claass")
for (let i = 0; i < reply.length; i++) {
suneditor.create(reply[i], {})
}
And.. maybe it is better to create a new editor each time unless the editors need to exist at the same time.
@ATOPBIO,
I use function like this:
function editor(id) {
SUNEDITOR.create(id, {
mode: 'classic',
...
});
In footer these JQ-each's works as you expect and make multiple instances of editors.
$(".record").each(function () {
editor($(this).attr("id")); // id = raw_1
});
PHP fragment:
<?php while(current($record)): ?>
<textarea class="sun-editor-editable record" id="<?=key($record)?>_<?=$record['id']?>">
<?php echo $record[key($record)]; next($record)?>
</textarea>
<?php endwhile; ?>