Skip to content

Instantly share code, notes, and snippets.

@vordan
Last active June 27, 2021 09:15
Show Gist options
  • Save vordan/954d90ae4c05ce5f2175 to your computer and use it in GitHub Desktop.
Save vordan/954d90ae4c05ce5f2175 to your computer and use it in GitHub Desktop.
Skipping to an element within the CKEditor by ID and setting the cursor focus within that element
// Skipping to an element within the CKEditor by ID and setting the cursor focus within that element.
// With the element in view, you'll attempt to insert the cursor at the beginning of the element using a Range.
// Firefox will actually insert the cursor for you but Chrome wont, so the Range step is necessary.
var element = evt.editor.document.getById('someHeading');
var range;
if(element) {
element.scrollIntoView();
// Thank you S/O
// http://stackoverflow.com/questions/16835365/set-cursor-to-specific-position-in-ckeditor
range = editor.createRange();
range.setStart(element, 0);
range.setEnd(element, 0);
editor.getSelection().selectRanges([range]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment