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

2 COMMENTS

Most Popular

Dominic
32688 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
7057 POSTS0 COMMENTS
Nicole Veronica
12198 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12293 POSTS0 COMMENTS
Shaida Kate Naidoo
7182 POSTS0 COMMENTS
Ted Musemwa
7447 POSTS0 COMMENTS
Thapelo Manthata
7160 POSTS0 COMMENTS
Umr Jansen
7150 POSTS0 COMMENTS