Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>This solution works in all major browsers:</strong></p> <p><code>saveSelection()</code> is attached to the <code>onmouseup</code> and <code>onkeyup</code> events of the div and saves the selection to the variable <code>savedRange</code>.</p> <p><code>restoreSelection()</code> is attached to the <code>onfocus</code> event of the div and reselects the selection saved in <code>savedRange</code>.</p> <p>This works perfectly unless you want the selection to be restored when the user clicks the div aswell (which is a bit unintuitative as normally you expect the cursor to go where you click but code included for completeness)</p> <p>To achieve this the <code>onclick</code> and <code>onmousedown</code> events are canceled by the function <code>cancelEvent()</code> which is a cross browser function to cancel the event. The <code>cancelEvent()</code> function also runs the <code>restoreSelection()</code> function because as the click event is cancelled the div doesn't receive focus and therefore nothing is selected at all unless this functions is run.</p> <p>The variable <code>isInFocus</code> stores whether it is in focus and is changed to "false" <code>onblur</code> and "true" <code>onfocus</code>. This allows click events to be cancelled only if the div is not in focus (otherwise you would not be able to change the selection at all).</p> <p>If you wish to the selection to be change when the div is focused by a click, and not restore the selection <code>onclick</code> (and only when focus is given to the element programtically using <code>document.getElementById("area").focus();</code> or similar then simply remove the <code>onclick</code> and <code>onmousedown</code> events. The <code>onblur</code> event and the <code>onDivBlur()</code> and <code>cancelEvent()</code> functions can also safely be removed in these circumstances.</p> <p>This code should work if dropped directly into the body of an html page if you want to test it quickly:</p> <pre><code>&lt;div id="area" style="width:300px;height:300px;" onblur="onDivBlur();" onmousedown="return cancelEvent(event);" onclick="return cancelEvent(event);" contentEditable="true" onmouseup="saveSelection();" onkeyup="saveSelection();" onfocus="restoreSelection();"&gt;&lt;/div&gt; &lt;script type="text/javascript"&gt; var savedRange,isInFocus; function saveSelection() { if(window.getSelection)//non IE Browsers { savedRange = window.getSelection().getRangeAt(0); } else if(document.selection)//IE { savedRange = document.selection.createRange(); } } function restoreSelection() { isInFocus = true; document.getElementById("area").focus(); if (savedRange != null) { if (window.getSelection)//non IE and there is already a selection { var s = window.getSelection(); if (s.rangeCount &gt; 0) s.removeAllRanges(); s.addRange(savedRange); } else if (document.createRange)//non IE and no selection { window.getSelection().addRange(savedRange); } else if (document.selection)//IE { savedRange.select(); } } } //this part onwards is only needed if you want to restore selection onclick var isInFocus = false; function onDivBlur() { isInFocus = false; } function cancelEvent(e) { if (isInFocus == false &amp;&amp; savedRange != null) { if (e &amp;&amp; e.preventDefault) { //alert("FF"); e.stopPropagation(); // DOM style (return false doesn't always work in FF) e.preventDefault(); } else { window.event.cancelBubble = true;//IE stopPropagation } restoreSelection(); return false; // false = IE style } } &lt;/script&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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