Categories
JavaScript Answers

How to use a HTML button to call a JavaScript function?

Spread the love

To use a HTML button to call a JavaScript function, we add a click event handler to it.

For instance, we write

<input id="clickMe" type="button" value="clickme" />

to add a button.

Then we write

document.getElementById("clickMe").onclick = () => {
  alert("hello!");
};

to select the button with getElementByid.

And we set its onclick property to a function that shows an alert box that shows hello! when we click the button.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *