how to call function in button javascript code example

Example 1: javascript onclick

document.getElementById("myBtn").addEventListener("click", function() {
  alert("Hello World!");
});

Example 2: how to call a function with a button in javascript

<script>
		function myFunction()
		{
			document.write("<h1>HELLO</h1>")
		}
	</script>
<button onclick="myFunction()">Click</button>

Example 3: run a function when a button has the onclick

<script>
function myFunction() {
  alert("ALERT THIS FUNCTION HAS RUNNED");
}
</script>
<button onclick="myFunction">click to run function</button>

Tags:

Misc Example