Ads by ProfitSence

Call javascript function from html Code Example

Last Updated on Wednesday 5th Oct 2022

Using an HTML button to call a JavaScript function

Defining in HTML

					<input id="tapMe" type="button" value="tapme" onclick="doFunction();" />


Adding it to the DOM property for the event in Javascript

					document.getElementById("tapMe").onclick = doFunction;

document.getElementById("tapMe").onclick = function () { alert('You Successfully Tap ME!'); };


Here's the small Example

  • Attaching a function to the event handler using Javascript
  <li class="red button">Red Button</li>
  <li class="blue button">Blue Button</li>
  <li class="green button">Green Button</li>

					const list = document.querySelector("ul");

list.addEventListener("click", (e) => { = "24px";
  if ("red")) { = "red";
  if ("blue")) { = "blue";
  if ("green")) { = "green";