title: Find selected text start and end index with JavaScript
body: <p>I have one ePub Book XHTML page with many p(paragraph) tags in it.</p>
  
  <p>Now I am getting selected text index with respect of body tag by below function in JS:</p>
  
  <pre><code>var fullString = document.getElementsByTagName("body")[0].textContent;
  if (window.getSelection) 
  {               
      var range = window.getSelection ();                     
      var startPosition = fullString.search(range);
      var getPosition = range.toString();
      var endPosition = parseInt(getPosition.length) + parseInt(startPosition);
      alert("Start position if : " + startPosition + " and End position : " + endPosition);
      start_position = startPosition;
      end_position = endPosition;                               
  }
  </code></pre>
  
  <p>It is returning me correct index while selection is in only one P tag.</p>
  
  <p>But as selection covers more than one p tag I am getting start position as "-1".</p>
  
  <p>Can you please help me to get out from this?</p>