How to call JavaScript function in html?17 Mar 2025 | 3 min read There are many ways to call a JavaScript function in the HTML document, and it is also not a difficult task. First, we have used one of the easiest ways to call a JavaScript function in HTML document: In this method, we will create and define a function in the HTML document's head section. To invoke this function in the html document, we have to create a simple button and using the onclick event attribute (which is an event handler) along with it, we can call the function by clicking on the button. To understand it more clearly let's see the given program: Program Explanation of program In the above-given program, we have created a simple HTML document. Inside the head section of the HTML document, we have defined a function (e.g myfunction();) inside the script tags <script>...</script>. On the other hand, inside the body section, we displayed some text and created a button. To call our function, we have used the onclick attribute along with the button and when the user clicks on that button our function gets executes and display an alert message, as you can see in the output. Output ![]() Calling a function using external JavaScript fileWe can also call JavaScript functions using an external JavaScript file attached to our HTML document. To do this, first we have to create a JavaScript file and define our function in it and save it with (.Js) extension. Once the JavaScript file is created, we need to create a simple HTML document. To include our JavaScript file in the HTML document, we have to use the script tag <script type = "text/javascript" src = "function.js"> and in the "src" attribute, we have to provide the path to our JavaScript file where it is stored. After linking the external JavaScript file to the HTML document, we can create a button and call the function using the "onclick" attribute with it. Let's understand it with help of a program: Program Explanation of program In the above program first, we have created a JavaScript file and defined our function in it and saved it with the .js extension. Function.js After creating the JavaScript file, we have created an HTML document and linked our JavaScript file using <script type = "text/javascript" src="function.js"></script>. Because we have stored our HTML document and JavaScript file in the same folder, we have just named our JavaScript file in the "scr" attribute instead of providing the full path in the head section Inside the body section, we displayed some text and created a button. To call our function, we have used the onclick attribute along with the button and when the user clicks on that button our function gets executes and display an alert message, as you can see in the output. Output ![]() Now click on the given button: ![]() Next TopicHow to write a function in JavaScript |
What is "Vanilla JavaScript"? The term vanilla script is used to refer to the pure JavaScript (or we can say plain JavaScript) without any type of additional library. Sometimes people often used it as a joke. Nowadays several things can also be done without using any...
3 min read
When the mouse cursor removes the HTML element, the onmouseout event is worked and works with user-defined functionality. This function is operated on the user element. html tag and div information when the mouse cursor goes away using event. The mouseout function works with the mouseout...
4 min read
displays the y-coordinates or height of the given element. The read-only MouseEvent offsetY attribute returns the y-coordinate of the mouse cursor for the target element. We can add the event function on the div tag, web page, or other elements to display y-coordination. Syntax The following...
3 min read
You must have noticed the efficiency of the web application's functionalities or how web applications handle user events. or JavaScript Throttle concept provides the mechanism to improve the performance and speed of the application. The throttling mechanism is also known as throttle method is a...
8 min read
JavaScript does not offer any specific in-built function to traverse the array elements/objects. You can traverse an array simply using for loop or directly by element index. An array contains multiple elements of the same type, which can be traverse using for loop. In this chapter,...
7 min read
iousSibling Property in Javascript The iousSibling property serves to get the ious or earlier node of the given node as a Node object. If the given node is the first item in the list, then the node object shows null. The iousSibling property displays the element that...
5 min read
We would have heard and learned the recursion concept when approaching different programming languages. In JavaScript also, we have the concept of Recursion, where we make use of the recursive functions. So, in this section, we are going to learn about Recursion and will also see...
6 min read
? A JavaScript function is a block of code that consists of a set of instructions to perform a specific task. A function can also be considered as a piece of code that can be used over again and again in the whole program, and it...
5 min read
The ECMAScript6 (ES6) provides the string repeat() feature to repeat the given string. The string can provide data to display the required time with function and event. The repeat method does not affect the original data, but displays require time the similar data. In other words,...
4 min read
This section will discuss the Fibonacci series and how we can generate the . Fibonacci series is a series that generates subsequent series of numbers by the addition of the two ious numbers. The first two terms of the Fibonacci series are zero and one, respectively....
5 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India