Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

301
Views
CKEditor 5-insert html and move the caret outside it

i have a button outside ckeditor, when clicked i want to insert a span element at caret position, this is what i have so far:

var el = document.querySelector('#editor');

ClassicEditor.create(el, {
    licenseKey: ''
}).then( editor => {
    appConfigs.editor = editor; //storing the editor for later usage
}).catch( error => {
    //...
});

when the button is clicked i do:

function clickHandler(){
    var editor = appConfigs.editor;

    editor.model.change( writer => {
        var html = '<span class="special-class-name" data-name="something">special word or button</span>';
        var viewFragment = editor.data.processor.toView(html);
        var modelFragment = editor.data.toModel(viewFragment);

        var insertPosition = editor.model.document.selection.getFirstPosition();

        editor.model.insertContent(modelFragment, insertPosition);
    });
}

the code have the following issues:

1)the inserted span get stripped off all its attributes(class, data-name) which i want to preserve.

2)the editing caret doesn't go back automatically to the editor when the button clicked.

3)when i click the editor to restore caret and start typing, i noticed it edits the text inside inserted span, i want inserted span to be untouched and any further typing goes after it.

i will be so grateful if anyone could help me with those issues.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!