Make Button Open a Link in a New Tab in JavaScript

In this tutorial, we will learn how to create a button that opens a link in a new tab with an onclick event.


Here is the full HTML and JavaScript code:


<button id="btn" type="button">Open Link</button>
document.getElementById('btn').addEventListener('click', function() {'');



In the example above, a button with an id of btn has a click Event Listener assigned to it and when the user clicks the button the JavaScript function is called, opening the link provided in a new tab.