How to call javascript function in HTML?

You can use the onclick attribute within an HTML element to trigger the function when the element is clicked, or you can use other event attributes like as onmouseover, onmouseout, and so on to execute the function in response to various events. Here's how you can accomplish it:

<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Call JavaScript Function in HTML</title>
// Define a JavaScript function
function myFunction() {
alert('Hello, world!'); // Replace this with your desired function code
<!-- Call the JavaScript function using onclick attribute -- >
<button onclick="myFunction()">Click Me</button>

Go from files to website in seconds.

Start a free trial for 7 days — no credit card required

Get Started for Free