To call a JavaScript function in HTML, add
onclick="myFunction()"
to a button
.First define your function inside a script
somewhere above the button
like so:
<script>
function myFunction() {
alert('Hello World!');
}
</script>
<button>I am a button!</button>
Then add the onclick
listener, to call your function when the button
is clicked:
<script>
function myFunction() {
alert('Hello World!');
}
</script>
<button onclick="myFunction()">I am a button!</button>