Thursday, December 26, 2024
Google search engine
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

Recent Comments