How to Create a Button in HTML Using JavaScript?

Estimated read time 1 min read

Here’s an example of how you could create a button in HTML using JavaScript:

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

<script>
  document.getElementById("myButton").addEventListener("click", function() {
    alert("Button was clicked!");
  });
</script>

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 display an alert with the message “Button was clicked!” when the button is clicked.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply