title: How can i get formatted HTML of selected part instead of simple text through this JS
body: <p>How can i get formatted HTML of selected part instead of simple text</p>
  <p>Here is a script that add content to <code>&lt;div class=&quot;container&quot;&gt; &lt;/div&gt; </code> when we select some text and press  <kbd>Get Selection</kbd> button, with every time class <code>mySlides</code> Okay</p>
  <p>But here is all text goes in one by one only in this structure, even though we select any HTML text like <code>&lt;p&gt; &lt;h2&gt;</code> etc part.</p>
  <pre><code>&lt;div class=&quot;container&quot;&gt;
  &lt;div class=&quot;mySlides&quot;&gt; Some Text &lt;/div&gt;
  &lt;div class=&quot;mySlides&quot;&gt; Some other &lt;/div&gt;
  &lt;div class=&quot;mySlides&quot;&gt; Some another &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  <p>But Now i want a little change here, my requirement is When i select text then it should be get with formatted rendered HTML Just Like This.</p>
  <pre><code>&lt;div class=&quot;container&quot;&gt;
  &lt;div class=&quot;mySlides&quot;&gt; Some Text &lt;/div&gt;
  
  &lt;div class=&quot;mySlides&quot;&gt; 
  &lt;h3&gt;This is heading 3&lt;/h3&gt;
       &lt;p&gt;
            Select any part of this sentence 
            and press the button. Select any part of this sentence 
            and press the button. Select any part of this sentence 
            and press the button
     &lt;/p&gt;
  &lt;/div&gt;
  
  &lt;div class=&quot;mySlides&quot;&gt; Some another &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  <p>i also checked <a href="https://stackoverflow.com/questions/4176923/html-of-selected-text">this</a> but i am not able to get solve my issue.
  means simple i want rendered HTML format of selection.</p>
  <p><strong>My Whole Code IS Here:</strong></p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const keySelected = new Set();
  
  document.addEventListener('keydown', (e) =&gt; {
      keySelected.add(e.which);
      if(keySelected.has(17) &amp;&amp; keySelected.has(90)){
          getSelectedText();
      }
  });
  
  document.addEventListener('keyup', (e) =&gt; {
       keySelected.delete(e.which);
  });
  
  
  // Function to get the Selected Text  
  function getSelectedText() {
    var selectedText = '';
    // #### create a new element with variable (nw) #### //
    var nw = document.createElement("div"); // Element's tag
    nw.className = "mySlides"; // Element's class name
     // some applied style
  
    // window.getSelection 
    if (window.getSelection) {
      selectedText = window.getSelection();
    }
    // document.getSelection 
    else if (document.getSelection) {
      selectedText = document.getSelection();
    }
    // document.selection 
    else if (document.selection) {
      selectedText =
        document.selection.createRange().text;
    } else return;
    // #### get the Selected text appended to body #### //
    nw.innerHTML = selectedText;
    document.getElementsByClassName('container')[0].prepend(nw); // Append element to body
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt;&lt;/script&gt;
  
    &lt;div class="container"&gt; &lt;/div&gt;
  
  &lt;button id="mybtn" onclick="getSelectedText()"&gt;Get Selection&lt;/button&gt;
  
    &lt;p&gt;
            Select any part of this sentence 
            and press the button. Select any part of this sentence 
            and press the button. Select any part of this sentence 
            and press the button
     &lt;/p&gt; 
     &lt;h3&gt;This is heading 3&lt;/h3&gt;
       &lt;p&gt;
            Select any part of this sentence 
            and press the button. Select any part of this sentence 
            and press the button. Select any part of this sentence 
            and press the button
     &lt;/p&gt; 
            </code></pre>
  </div>
  </div>
  </p>
  <p>Plz Help Me How to do that.</p>