How to Create a Button Click Event to Open a New HTML Page in JavaScript?

Estimated read time 1 min read

Here’s an example of how you can create a button click event to open a new HTML page in JavaScript:

<button id="myButton">Click Me!</button>

  document.getElementById("myButton").addEventListener("click", function() {
    window.location.href = "newpage.html";

In this example, we use the document.getElementById method to select the button with the id of “myButton”. We then use the addEventListener method to attach a click event to the button, which will change the current window location to the specified URL, in this case “newpage.html”. This will open the new HTML page in the same tab or window.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply