Friday, September 5, 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
32264 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6634 POSTS0 COMMENTS
Nicole Veronica
11801 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11863 POSTS0 COMMENTS
Shaida Kate Naidoo
6750 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6701 POSTS0 COMMENTS
Umr Jansen
6718 POSTS0 COMMENTS