Set the cursor to complete after focusing CKEditor

Possible duplicate:
CKEditor - Set cursor position to end of text

I have a <div> with lots of content. After clicking on this div, CKEditor loads to edit this div.

Now I would like to set the cursor / cursor at the end of the content after replacing it with an editor.

My code currently looks something like this:

 var editor = CKEDITOR.replace('content', { // Settings // Event listeners on: { instanceReady: function(evt) { var editor = evt.editor; // give focus (displays caret at the beginning of the content, not the end) editor.focus(); } } }); 
+6
source share
1 answer

After messing around a bit, I need it to work with the following code:

 $(document).ready(function() { CKEDITOR.on('instanceReady', function(ev) { ev.editor.focus(); var s = ev.editor.getSelection(); // getting selection var selected_ranges = s.getRanges(); // getting ranges var node = selected_ranges[0].startContainer; // selecting the starting node var parents = node.getParents(true); node = parents[parents.length - 2].getFirst(); while (true) { var x = node.getNext(); if (x == null) { break; } node = x; } s.selectElement(node); selected_ranges = s.getRanges(); selected_ranges[0].collapse(false); // false collapses the range to the end of the selected node, true before the node. s.selectRanges(selected_ranges); // putting the current selection there } }); 

Idea:

  • Get root node (not body)
  • Continue to the next node until there are no more nodes to go to.
  • Select the last node.
  • Collapse it
  • Setting range
+4
source

Source: https://habr.com/ru/post/898648/


All Articles