Search code examples
javascriptjqueryhtmlckeditor

CKEDITOR : Remove selection and set cursor position at the end of selection


I am facing some selection related issued in CKEDITOR.

MY GOAL :

Remove Selection

Set cursor position at the end of the selection

MY CODE :

editor.getSelection().removeAllranges();  //Works Fine remove selection

//Try to focus on editor
editor.focus();  // Not Working

How can I solve the Problem Guys?

Please help me to do this.


Solution

  • This code will get the last range in your current selection, create a range starting and ending at the end position of the last range in your current selection, and select that range.

    var oldRanges = editor.getSelection().getRanges();
    var oldRange = oldRanges[oldRanges.length - 1];
    var newRange = editor.createRange();
    
    newRange.setStart(oldRange.endContainer, oldRange.endOffset);
    newRange.setEnd(oldRange.endContainer, oldRange.endOffset);
    
    editor.getSelection().selectRanges([ newRange ]);