Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here's a simple example of how to retain the selection when the user clicks a particular element. It stores the selection <code>Range</code>(s) or <code>TextRange</code> (IE &lt;= 8) in the <code>mousedown</code> event of the element and reselects those ranges in the <code>mouseup</code> event.</p> <pre><code>&lt;script type="text/javascript"&gt; var saveSelection, restoreSelection; if (window.getSelection) { // IE 9 and non-IE saveSelection = function() { var sel = window.getSelection(), ranges = []; if (sel.rangeCount) { for (var i = 0, len = sel.rangeCount; i &lt; len; ++i) { ranges.push(sel.getRangeAt(i)); } } return ranges; }; restoreSelection = function(savedSelection) { var sel = window.getSelection(); sel.removeAllRanges(); for (var i = 0, len = savedSelection.length; i &lt; len; ++i) { sel.addRange(savedSelection[i]); } }; } else if (document.selection &amp;&amp; document.selection.createRange) { // IE &lt;= 8 saveSelection = function() { var sel = document.selection; return (sel.type != "None") ? sel.createRange() : null; }; restoreSelection = function(savedSelection) { if (savedSelection) { savedSelection.select(); } }; } window.onload = function() { var specialDiv = document.getElementById("special"); var savedSel = null; specialDiv.onmousedown = function() { savedSel = saveSelection(); }; specialDiv.onmouseup = function() { restoreSelection(savedSel); }; }; &lt;/script&gt; &lt;p&gt; Select something up here and click on one of the areas below. &lt;b&gt;The first will lose the selection&lt;/b&gt;, while the second will keep it. &lt;/p&gt; &lt;div style="border: solid blue 1px"&gt;Normal div&lt;/div&gt; &lt;div id="special" style="border: solid blue 1px"&gt;Special div. Press me and keep the selection&lt;/div&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload