Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to call JavaScript function in HTML ?

  • Last Updated : 06 Aug, 2021

In this article, we will see how to call JavaScript function in HTML with two approaches that are discussed below.

Approach 1: First, take a button by the input tag. After clicking the button, you can see a dialog box that pops up on the screen that has already been declared in the JavaScript function as an alert. The clickEvent() function allows executing the alert() when this button gets clicked by using onclick() method.

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

Example:

HTML




<!DOCTYPE html>
<html>
  
<body>
    <input type="button" onclick="clickEvent();"
        value="button" />
    <script>
        function clickEvent() {
            alert("Whoo!! You have discovered");
        }
    </script>
</body>
  
</html>

Output:



Approach 2: By using the display() method in JavaScript, you can see the output GeeksForGeeks in the h1 tag.

Example:

HTML




<!DOCTYPE html>
<html>
  
<head>
    <title>Call JavaScript function in HTML</title>
</head>
  
<body>
    <script>
        function display() {
            document.write("<h1>GeeksForGeeks</h1>");
        }
        display();
    </script>
</body>
  
</html>

Output:

h1 tag




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!