javascriptjqueryckeditorckeditor5

Get the highlighted/selected text in CKEDITOR 5


I have my Inline CKeditor

let globalEditor;

InlineEditor.create(document.querySelector("#textarea"), {
        toolbar: {
            items: ['heading', '|', 'bold', 'italic', 'link', 'bulletedList', 'numberedList', 'blockQuote', 'insertTable', 'undo', 'redo']
        }
}).then(editor => {
        globalEditor = editor;
}).catch(err => {
    console.error(err.stack);
});

I also have a button that supposed to be getting the highlighted/selected text inside the ckeditor

 $("#btnAddTag").click(function (e) {
        e.preventDefault();
        var editor = globalEditor;

        var getText = editor.getSelection().getNative(); //I tried this but the *getSelection* is undefined
 });

Any suggestions?


Solution

  • Already fixed the problem

    const editor = globalEditor;
    const selection = editor.model.document.selection;
    const range = selection.getFirstRange();
    
    for (const item of range.getItems()) {
        console.log(item.data) //return the selected text
    }