There are multiple ways to call a JavaScript function using an HTML button for different situations and they are:
Defining the function in HTML:
<input id="click" type="button" value="click" onclick="Function();" />
Adding it to a DOM property for an event:
document.getElementById(“click”).onclick =function;
Or
document.getElementById(“click”).onclick=fuction(){
//Something the function does
};
You can also use an event handler :
var element=document.getElementById(“click”);
if(element.addEventListener)
element.addEventListener(“click”,doFunction,false);
else if(element.attachEvent)
element.attachEvent(‘onclick’,doFunction);
Check out these Web Development online courses to enhance your career!