title: How to select div text on button click?
body: <p>I want to select the div content on click button. </p>
  
  <p>HTML</p>
  
  <pre><code> &lt;div id="divid"&gt;Hello This div content have to be select. &lt;/div&gt;
   &lt;button onclick="selectText(divid);"&gt; Select Div&lt;/button&gt;
  </code></pre>
  
  <p>JS</p>
  
  <pre><code>   function selectText(divid) {
          if (document.selection) {
              var div = document.body.createTextRange();
  
              div.moveToElementText(document.getElementById("divid"));
              div.select();
          }
          else {
              var div = document.createRange();
              div.setStartBefore(document.getElementById("divid"));
              div.setEndAfter(document.getElementById("divid"));
  
              window.getSelection().addRange(div);
          }
  
      }
  </code></pre>
  
  <p><a href="https://jsfiddle.net/rajagopalx/xds4y0en/" rel="nofollow">https://jsfiddle.net/rajagopalx/xds4y0en/</a></p>