run a function when a button has the onclick code example

Example 1: how to add onclick event in javascript

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

Example 2: button functions html

<script>
 function myFunction() {
   alert("ALERT");
 }
</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>

Example 4: javascript onclick

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

Example 5: calling function on click html

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

Tags:

Css Example