• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / How to Call a JavaScript Function From HTML

How to Call a JavaScript Function From HTML

January 18, 2022 Leave a Comment

To call a JavaScript function from HTML, we can add an onclick event in our HTML code that will fire a function when the HTML element is clicked on.

<div id="div1" onclick="funct1()">Click me to run the function funct1</div>

We can also use the onmouseover event to call a function when the user moves a mouse over an HTML element.


<div id="div2" onmouseover="funct2()">Move the mouse over to run the function funct2</div>

There are other ways you can call a JavaScript function in HTML, but these are the two methods we find the easiest and most practical.

Using the onclick event to call a JavaScript function in HTML

Below is an example of how we can create a function that adds two numbers, and then execute the function when a user clicks on a button we create. We will call the JavaScript function we create using an onclick event that we will attach to the HTML button.

<p>Enter two numbers below. Click Submit to have the added value of the numbers displayed:</p>
<input id="userVal1" type="text" value1="">
<input id="userVal2" type="text" value2="">
<button onclick="addNumbers()">Submit</button>
<div id="results"></div>

Below we will write our function. Because the button has onclick=”addNumbers()”, the addNumbers function, which we will create below, will run when the button is clicked. The function will add the two numbers that the user enters together and display the result. If something other than a number is entered, NaN will be returned.

function addNumbers() {
  var num1 = document.getElementById("userVal1").value;
  var num2 = document.getElementById("userVal2").value;
  var total = Number(num1) + Number(num2);
  document.getElementById("results").textContent = total;    
}

The final code and output for this example is below:

Code Output:

Enter two numbers below. Click Submit to have the added value of the numbers displayed:



Full Code:

<p>Enter two numbers below. Click Submit to have the added value of the numbers displayed:</p>
<input id="userVal1" type="text" value1="">
<input id="userVal2" type="text" value2="">
<button onclick="addNumbers()">Submit</button>
<div id="results"></div>

<script>

function addNumbers() {
  var num1 = document.getElementById("userVal1").value;
  var num2 = document.getElementById("userVal2").value;
  var total = Number(num1) + Number(num2);
  document.getElementById("results").textContent = total;    
}

</script>

Using the onmouseover event to call a JavaScript function in HTML

Finally, we will create a simple example to show how to fire a function from HTML with the onmouseover event. We will have a rectangle div with a green background. When the user hovers over the box, we will change the background color to blue. Here is the HTML code:

<style>#rect1 { background: green; width: 300px; height: 100px; }</style>
<p>Hover over the box below to change its color.</p>
<div id="rect1" onmouseover="changeBGcolor()"></div>

When the user hovers over our div, we will run the

via the onmouseover event. The function will work like this:

function changeBGcolor() {
  document.getElementById("rect1").style.backgroundColor = 'blue';    
}

The final code and output for this example is below:

Code Output:

Hover over the box below to change its color.

Full Code:

<style>#rect1 { background: green; width: 300px; height: 100px; }</style>
<p>Hover over the box below to change its color.</p>
<div id="rect1" onmouseover="changeBGcolor()"></div>

<script>

function changeBGcolor() {
  document.getElementById("rect1").style.backgroundColor = 'blue';    
}

</script>

Hopefully this article has been useful in helping you learn how to call a javascript function from HTML.

Other Articles You'll Also Like:

  • 1.  Convert String to Array in JavaScript
  • 2.  How to Repeat a String in JavaScript
  • 3.  Using JavaScript to Check if Variable is an Object
  • 4.  Remove Parentheses From String Using JavaScript
  • 5.  Remove Word From String In JavaScript
  • 6.  Insert Character Into String In JavaScript
  • 7.  JavaScript sin – Find Sine of Number in Radians Using Math.sin()
  • 8.  Using JavaScript to Insert Item Into Array
  • 9.  Using JavaScript to Get the Height of an Element
  • 10.  JavaScript value – Get the Value from an Input Field

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy

x