Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (7k points)
I am new to web development and I do not know how to call a JavaScript function using an HTML button. Can someone tell me what are the different ways to do this?

1 Answer

0 votes
by (13.1k points)
edited by

There are multiple ways to call a JavaScript function using an HTML button for different situations and they are:

  1. Defining the function in HTML:

 <input id="click" type="button" value="click" onclick="Function();" />

  1. Adding it to a DOM property for an event:

document.getElementById(“click”).onclick =function;

Or 

document.getElementById(“click”).onclick=fuction(){

//Something the function does

};

  1. 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!

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...