HubSpot Inc.

11/18/2024 | News release | Distributed by Public on 11/18/2024 06:11

How to Call a JavaScript Function in HTML

How to Call a JavaScript Function in HTML

Updated: November 18, 2024

Published: January 09, 2023

Once you have a good grasp of HTML and CSS, the next step is JavaScript to take your web projects to the next level. JavaScript adds interactive and dynamic features to web pages - it's what makes them interesting and engaging beyond just static content.

If you want to make an interactive page, one of the fundamental skills you need to know is how to call JavaScript functions from your HTML document. In other words, when a user engages with an HTML element in some way (like clicking it), how can you trigger a JavaScript function in response?

Fortunately, HTML and JavaScript make this easy. In this post, you'll learn three methods to call a JavaScript function with HTML. Let's dive in.

How to Call a JavaScript Function in HTML

In this section, we'll go over three ways to call a function with HTML

How To Land a Developer Role in the World of AI

A free checklist to you help you stand out from the competition featuring Software Developer and YouTube Creator Tech With Tim.

  • Expert advice on how to build a portfolio
  • Top programming languages to learn
  • Resume building and interview tips
  • Resources to strengthen communication skills
Download for Free Learn more

Download Free

All fields are required.

You're all set!

Click this link to access this resource at any time.

Use Script Tags

The simplest way to call a JavaScript function in an HTML document is to define the function inside a pair of

And here's what the HTML and JavaScript code look like in action:

See the Pen call function in js: external js by HubSpot (@hubspot) on CodePen.

Use Event Listeners

The final and most powerful way to call JavaScript functions with HTML is with event listeners. In JavaScript, an event listener is a feature that waits for an event (such as a mouse click) to occur on an element or set of elements. Once that event occurs, the event listener calls a function.

To create an event listener in JavaScript, you use the built-in addEventListener() function. This function adds an event listener to an HTML element, class, or id. The addEventListener() function takes two required parameters:

  • event: This triggers the listener, like click or mouseover.
  • listener: This is JavaScript function that is called when the event occurs. You can add multiple listeners if desired.

In the example below, we've set up an event listener for a click event. When the button is clicked, the event listener calls changeColor().

See the Pen call function in js: event listener by HubSpot (@hubspot) on CodePen.

Event listeners are ideal for triggering JavaScript functions with user input because they remove the need to include JavaScript functioncalls (i.e., onclick) from your HTML, making code easier to maintain. Plus, you can call multiple functions from a single event.

Call JavaScript functions with HTML.

At it's core, HTML is responsible for the content of the page alone. Without JavaScript, users won't be able to do much interaction with your web page.

However, by mastering the techniques we've covered in this post, you can immediately increase on-page engagement. Experiment with these different techniques, and see which works best for your project and coding style.

How To Land a Developer Role in the World of AI

A free checklist to you help you stand out from the competition featuring Software Developer and YouTube Creator Tech With Tim.

  • Expert advice on how to build a portfolio
  • Top programming languages to learn
  • Resume building and interview tips
  • Resources to strengthen communication skills
Download for Free Learn more

Download Free

All fields are required.

You're all set!

Click this link to access this resource at any time.

Download Now
Topics: HTML

Don't forget to share this post!