title: Get Selected HTML in browser via Javascript
body: <p>I have a requirement for my web app to allow the user to "Print Selected Only". In other words, a user selects text and potentially images and then clicks this option. I've seen examples of getting selected text with Javascript, but haven't found a solution for getting the selected html itself. </p>
  
  <p>As an example if I have a document like so:</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;p&gt;A bunch of text&lt;/p&gt;
      &lt;img src="someimage.jpg" /&gt;
      &lt;p&gt;Even more text&lt;/p&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>If user highlights the image and the second paragraph, I'd want the javascript to return:</p>
  
  <pre><code>&lt;img src="someimage.jpg" /&gt;
  &lt;p&gt;Even more text&lt;/p&gt;
  </code></pre>
  
  <p>Is this possible and how would one go about doing it?</p>
  
  <p>Edit: I ended up going with a js library called <a href="https://github.com/timdown/rangy" rel="noreferrer">Rangy</a> for this.</p>