call function in onclick javascript code example

Example 1: set onclick javascript

<button id="myBtn">Try it</button>
<p id="demo"></p>

<script>
document.getElementById("myBtn").onclick=function(){displayDate()};

function displayDate()
{
    document.getElementById("demo").innerHTML=Date();
}
</script>

Example 2: javascript onclick

document.getElementById("myBtn").addEventListener("click", function() {
  alert("Hello World!");
});

Example 3: calling function on click html

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

Tags:

Misc Example