Home >Web Front-end >JS Tutorial >How to call JavaScript functions in HTML?

How to call JavaScript functions in HTML?

PHPz
PHPzforward
2023-09-14 09:53:041514browse

In this article, we will explore calling and initializing JavaScript functions from HTML templates. We need a JavaScript function to perform the required method on the passed input.

In this tutorial, we will discuss the two main methods of calling JavaScript functions from HTML pages.

In the first method, we will take a simple input tag and a submit button associated with it. After clicking the button, we will see a dialog box pop up on the screen as an alert. Clicking this button calls a JavaScript function to display the alert.

Method 1

  • First, select a button next to the input tag.

  • After clicking the button, you can see A dialog box pops up on the screen that has been declared as an alert in a JavaScript function.

  • The clickEvent() function allows an alert() to be executed when this button is clicked using the onclick() method.

Example 1

#index.html

<!DOCTYPE html>
<html>
<body>
   <h1 style="color: green;">
      Welcome To Tutorials Point
   </h1>
   <input type="button" onclick="clickEvent();" value="Click Here !" />
   <script>
      function clickEvent() {
         alert("Hey... The Javascript function is invoked.");
      }
   </script>
</body>
</html>

Output

The above program will produce the following result. When you click the "Click here!" button it will alert and display the message "Hey...Javascript function called."

如何在 HTML 中调用 JavaScript 函数?

The above is the detailed content of How to call JavaScript functions in HTML?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete