Home > Web Front-end > JS Tutorial > body text

How to call JavaScript functions in HTML?

PHPz
Release: 2023-09-14 09:53:04
forward
1450 people have browsed it

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>
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!