title: How to make button open a link in new tab in JavaScript?
body: <p>I want to create a <code>&lt;button&gt;</code> that when clicked will open a URL in a new tab.</p>
  
  <p>This is easy to achieve in pure HTML, but the catch is that the <code>&lt;button&gt;</code> is dynamically generated, thus this has to be done in JavaScript.</p>
  
  <p>This is the code I used to create a <code>&lt;button&gt;</code> so far using JavaScript and HTML DOM:</p>
  
  <pre><code>&lt;body&gt;
    &lt;div id = 'only_div'&gt;
    &lt;/div&gt;
    &lt;script&gt;
      var btn=document.createElement("button");
      btn.innerText = "Click me";
      document.getElementById("only_div").appendChild(btn);
    &lt;/script&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p>In this existing code, I just want to add that when clicked, a URL (for example <code>http://www.stackoverflow.com</code>) will open in a new tab.</p>