Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to call javascript function in HTML

Getting Started

One of the core features that makes JavaScript a powerful language is its ability to be integrated seamlessly into HTML. This allows us to create dynamic and interactive web pages. This article will walk you through the process of calling a JavaScript function in HTML.

Understanding JavaScript Functions

Before we dive into how to call a JavaScript function in HTML, let's take a moment to understand what a JavaScript function is. In simple terms, a function is a block of code designed to perform a particular task. You can think of a function as a machine in a factory: you give it some input, it performs an operation, and then it gives you some output.

Here's a simple JavaScript function that adds two numbers:

function addNumbers(num1, num2) {
    var result = num1 + num2;
    return result;
}

The function addNumbers takes two inputs (or parameters), num1 and num2, adds them together, and then returns the result.

Integrating JavaScript into HTML

There are three main ways that you can integrate JavaScript into an HTML document:

Inline JavaScript: This is when you include your JavaScript code directly inside your HTML tags. This is generally not recommended because it can make your HTML document look messy and it makes it harder to manage your JavaScript code.

Internal JavaScript: This is when you include your JavaScript code inside a <script> tag within your HTML document. This is better than inline JavaScript because it keeps your JavaScript code separate from your HTML code.

External JavaScript: This is when you include your JavaScript code in a separate file and then link to that file from your HTML document. This is the recommended approach because it keeps your JavaScript code completely separate from your HTML code, making both easier to manage.

Calling a JavaScript Function in HTML

Inline JavaScript

Let's start with the simplest (though not recommended) method of calling a JavaScript function in HTML: inline JavaScript. Here's how you could call our addNumbers function from an HTML button:

<button onclick="addNumbers(1, 2)">Click me!</button>

When you click the button, it will call the addNumbers function with 1 and 2 as parameters.

Internal JavaScript

To call a JavaScript function from HTML using internal JavaScript, you would first define your function inside a <script> tag, and then call it from your HTML code:

<script>
function addNumbers(num1, num2) {
    var result = num1 + num2;
    alert(result);
}
</script>

<button onclick="addNumbers(1, 2)">Click me!</button>

With this code, when you click the button, it will call the addNumbers function with 1 and 2 as parameters, and then display the result in an alert box.

External JavaScript

Finally, let's look at how to call a JavaScript function from HTML using external JavaScript. First, you would define your function in a separate JavaScript file:

// in script.js
function addNumbers(num1, num2) {
    var result = num1 + num2;
    alert(result);
}

Then, you would link to that file from your HTML document and call the function from your HTML code:

<script src="script.js"></script>

<button onclick="addNumbers(1, 2)">Click me!</button>

Just like before, when you click the button, it will call the addNumbers function with 1 and 2 as parameters, and then display the result in an alert box.

Conclusion

Calling a JavaScript function from HTML is a straightforward process once you understand the basic principles. Remember to keep your JavaScript code separate from your HTML to make it easier to manage. Happy coding!