Forked from israelcrux/save-and-restore-contenteditable-selection.js
Created
December 11, 2018 14:26
-
-
Save gitSambhal/d305b2b5442c5019f2b5caf2bf28a40d to your computer and use it in GitHub Desktop.
Revisions
-
Israel Cruz revised this gist
Dec 9, 2016 . 1 changed file with 17 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -22,4 +22,21 @@ function restoreSelection(range) { } } /** * How to use: * You have a text editor, or a textarea, or any text element, then you want to create a widget * that adds a link, or anything that causes a new element to get focus so your text element looses it and * selection is lost, then you may want to restore that selection after. */ var selectionRange = saveSelection(); // then, you loose focus /** * You get what you wanted and you want your selection back there */ restoreSelection(selectionRange); // Credits: Tim Down's SO answer http://stackoverflow.com/a/3316483/1470564 -
Israel Cruz created this gist
Dec 9, 2016 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,25 @@ function saveSelection() { if (window.getSelection) { var sel = window.getSelection(); if (sel.getRangeAt && sel.rangeCount) { return sel.getRangeAt(0); } } else if (document.selection && document.selection.createRange) { return document.selection.createRange(); } return null; } function restoreSelection(range) { if (range) { if (window.getSelection) { var sel = window.getSelection(); sel.removeAllRanges(); sel.addRange(range); } else if (document.selection && range.select) { range.select(); } } } // Credits: Tim Down's SO answer http://stackoverflow.com/a/3316483/1470564