Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to call a JavaScript function in HTML

When learning programming, one of the first languages you might encounter is JavaScript. It's a versatile and widely used language for web development. In this blog post, we will explore how to call a JavaScript function in HTML. We'll cover the basics, provide examples, and explain the concepts in a beginner-friendly way.

What is a function?

Before we dive into calling JavaScript functions in HTML, let's first discuss what a function is. In programming, a function is a block of code that performs a specific task. Functions are reusable, which means you can call the same function multiple times with different inputs to achieve various outcomes. This helps to keep your code organized, modular, and easier to maintain.

To better understand functions, think of them as a recipe in a cookbook. Each recipe has specific ingredients (inputs) and steps (code) that, when followed, produce a particular dish (output). Just like how you can use the same recipe to cook multiple times, you can use functions to perform the same task multiple times, as long as you provide the necessary inputs.

What is JavaScript?

JavaScript is a programming language commonly used for web development. It allows you to add interactivity and dynamic content to your web pages. JavaScript code can be embedded directly into an HTML file or included as an external file.

How to call a JavaScript function in HTML?

Now that you have a basic understanding of JavaScript and functions let's explore the different ways to call a JavaScript function in HTML.

Method 1: Inline JavaScript

The simplest way to call a JavaScript function in HTML is by writing the JavaScript code directly within the HTML file using the <script> tag. The <script> tag should be placed within the <head> or <body> of the document.

Here's an example of how to define and call a simple JavaScript function using inline JavaScript:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Inline JavaScript</title>
    <script>
        function greet() {
            alert("Hello, World!");
        }
    </script>
</head>
<body>
    <button onclick="greet();">Click me!</button>
</body>
</html>

In this example, we define a function called greet() inside the <script> tag. The function displays an alert with the message "Hello, World!" when called. We then create a button using the <button> tag and use the onclick attribute to call the greet() function when the button is clicked.

Method 2: External JavaScript file

A more organized and cleaner approach is to separate the JavaScript code from the HTML by placing it in an external file. This way, you can keep your HTML and JavaScript code separate, making it easier to manage and maintain.

To call a JavaScript function from an external file, follow these steps:

  1. Create a new file with a .js extension (e.g., main.js).
  2. Write your JavaScript function in the .js file.
  3. Link the external JavaScript file to your HTML file using the <script> tag and the src attribute.

Here's an example of how to define a function in an external JavaScript file and call it from an HTML file:

main.js

function greet() {
    alert("Hello, World!");
}

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>External JavaScript</title>
    <script src="main.js"></script>
</head>
<body>
    <button onclick="greet();">Click me!</button>
</body>
</html>

In this example, we define the greet() function in the main.js file and link it to the index.html file using the <script> tag with the src attribute. The function is then called using the onclick attribute on the <button> tag, just like in the previous example.

Method 3: Event listeners

Another way to call a JavaScript function in HTML is by using event listeners. Event listeners are a more modern and flexible approach to handling events (such as clicking a button) compared to using inline event handlers like onclick.

To use an event listener, you need to:

  1. Assign an id attribute to the HTML element that will trigger the function (e.g., a button).
  2. Write a JavaScript function that will be called when the event occurs.
  3. Use the addEventListener() method in your JavaScript code to bind the function to the event.

Here's an example of how to call a JavaScript function using an event listener:

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Event Listener</title>
    <script src="main.js"></script>
</head>
<body>
    <button id="greetButton">Click me!</button>
</body>
</html>

main.js

function greet() {
    alert("Hello, World!");
}

document.getElementById("greetButton").addEventListener("click", greet);

In this example, we assign an id attribute called greetButton to the <button> tag in the HTML file. In the JavaScript file, we define the greet() function and use the addEventListener() method to bind the greet() function to the click event of the button with the greetButton id.

Conclusion

In this blog post, we covered three methods to call a JavaScript function in HTML:

  1. Inline JavaScript
  2. External JavaScript file
  3. Event listeners

As a beginner, it's essential to understand these different methods and choose the most appropriate approach depending on your project's needs and your personal preferences. Separating JavaScript code from HTML using external files and event listeners is generally considered a better practice for larger projects, as it makes the code more modular, maintainable, and easier to understand.