title: How can I get the element in which highlighted text is in?
body: <p>I am trying to learn how to write a bookmarklet where I can highlight some text, click on the bookmarklet and have it tell me what got highlighted.  I can get that far, but next I want to know what element that text is in.</p>
  
  <p>For example:</p>
  
  <pre><code>&lt;div id="some-id"&gt;to be highlighted&lt;/div&gt;
  </code></pre>
  
  <p>The bookmarklet code:</p>
  
  <pre><code>javascript:(function(){alert(window.getSelection();})()
  </code></pre>
  
  <p>If I highlight the text "to be highlighted" and then click on the bookmarklet, it will alert the text.  But how can I get the element in which the text is in, in this case the  element after that?</p>
  
  <p>So the flow is: highlight text, click bookmarklet, bookmarklet tells you what you highlighted and the element it's in.</p>
  
  <p>Thanks!</p>