Skip to content
Related Articles

Related Articles

How to call JavaScript function in HTML ?

Improve Article
Save Article
Like Article
  • 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.

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!