html button call js function 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: onclick a tag

<a href='http://www.google.com' onclick='return check()'>check</a>

<script type='text/javascript'>
function check() {
    return false;
}
</script>

Example 3: javascript onclick

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

Example 4: javascript onclick button

var button = document.querySelector('button');
button.onclick = function() {
  //do stuff
}

Example 5: button click function in js

<script>
  $(document).ready(function(){
    $('#MyButton').click(function(){
       CapacityChart();
    });
  });
</script>

<input type="button" value="Capacity Chart" id="MyButton" >

Example 6: calling function on click html

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

Tags:

Java Example