Check out example codes for "run a function when a button has the onclick". It will help you in understanding the concepts better.

Code Example 1

<script>
 function myFunction() {
   alert("ALERT");
 }
</script>
<button onclick="myFunction">Click</button>

Code Example 2

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

Code Example 3

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

Code Example 4

var element = document.getElementById("elem");
element.onclick = function(event) {
  console.log(event);
}

Code Example 5

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

Learn ReactJs, React Native from akashmittal.com