how to link html button to javascript function code example

Example 1: onclick a tag

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

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

Example 2: 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>

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" >

Tags:

Css Example