Friday, October 10, 2025
HomeUncategorisedHow to call JavaScript function in HTML ?

How to call JavaScript function in HTML ?

In this article, we will see how to call JavaScript functions in HTML, A function is a set of statements that take inputs, do some specific computation, and produce output. we have two common 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: This example shows the use of the above-explained approach.

HTML




<!DOCTYPE html>
<html lang="en">
<head>
    <title>
        How to call JavaScript function in HTML ?
    </title>
</head>
 
<body>
    <input type="button" onclick="clickEvent();"
           value="button" />
    <script>
        function clickEvent() {
            alert("Who!! 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: The below example demonstrate the above approach.

Javascript




function display() {
    console.log("GeeksForGeeks");
}
display();


Output:

GeeksForGeeks
RELATED ARTICLES

Most Popular

Dominic
32350 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6718 POSTS0 COMMENTS
Nicole Veronica
11880 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6838 POSTS0 COMMENTS
Ted Musemwa
7101 POSTS0 COMMENTS
Thapelo Manthata
6794 POSTS0 COMMENTS
Umr Jansen
6794 POSTS0 COMMENTS