首页 > 解决方案 > 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?

标签: javascriptjqueryckeditorckeditor5

解决方案


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
}  

推荐阅读