call function in javascript on button click code example

Example 1: 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 2: javascript onclick

document.getElementById("Save").onclick = function ()
    {
     alert("hello");
     //validation code to see State field is mandatory.  
    }

Example 3: javascript onclick

var myElem = document.getElementByID('ElemID');
myElem.onclick = function() {
	//do stuff
}

Example 4: calling function on click html

<img src="hospital.png" id="hospitals" onclick="damarkers();">

Tags:

Misc Example