title: javascript: Alternative for selectionStart and selectionEnd for old versions of IE
body: <p>I have put together a script for grabbing a selection from a string or html in some sort of element such as as a div or span and formatting it with help from MaxArt that works great for standards-compliant browsers.  However, for older versions of IE, while I can get the selection, I don't have a way to get the start and end points.  The start and end points are necessary to put the selection, once manipulated back into the string.</p>
  
  <p>SelectionStart or SelectionEnd work for textareas but not divs or spans.  The following courtesy of MaxArt works in standards-compliant browers.  Can anyone recommend something that would work in older versions of IE for span and div.</p>
  
  <p>Thanks!</p>
  
  <p>JS</p>
  
  <pre><code>function editText() {
  //var sel = document.getElementById('displaytext').textContent;
  //alert(sel);
      var sel = getSelection();
      alert(sel);
      var rng, startSel, endSel, sel;
  if (!sel.rangeCount
          || displaytext.compareDocumentPosition((rng = sel.getRangeAt(0)).startContainer) === Node.DOCUMENT_POSITION_PRECEDING
      || displaytext.compareDocumentPosition(rng.endContainer) === Node.DOCUMENT_POSITION_FOLLOWING ) {
      sel = "";
  }
  else {
      startSel = displaytext.compareDocumentPosition(rng.startContainer) === Node.DOCUMENT_POSITION_FOLLOWING ? 0 : rng.startOffset;
      endSel = displaytext.compareDocumentPosition(rng.endContainer) === Node.DOCUMENT_POSITION_PRECEDING ? displaytext.textContent.length : rng.endOffset;
      sel = displaytext.textContent.substring(startSel, endSel);
  }
  }
  </code></pre>
  
  <p>HTML</p>
  
  <pre><code>&lt;span id = "displaytext"&gt;HERE IS A BUNCH OF TEXT THAT I WANT TO GRAB A SELECTION FROM&lt;/span&gt; &lt;a href="javascript:void(0)" onclick="editText()"&gt;link&lt;/a&gt;
  </code></pre>