Site icon TestingDocs.com

Pass values to a JavaScript Function

Overview

In this post, we will develop a simple JavaScript function and learn how to pass values to a JavaScript function. The script is simple add function. However, we will pass the number of digits that needs to appear after decimal to the add function.

HTML Code

Enter the First number:  
Enter the Second number:  


  

www.TestingDocs.com | 2016

JavaScript Function

function add(decimaldigits) {
var aLocal = parseFloat(document.getElementById("a").value);
var bLocal = parseFloat(document.getElementById("b").value);
alert(aLocal);
var result = aLocal + bLocal;
resultStr = result.toFixed(decimaldigits) + "<br>";
document.getElementById("output").innerHTML = resultStr;
}

Sample Output

To run the JavaScript code, open the HTML file in the browser window.

Add the two numbers in the textboxes and hit on the Add button.

The output would be displayed in the output div element.

JavaScript tutorials on this website can be found at:
https://www.testingdocs.com/javascript-tutorial/

To learn more about JavaScript, visit

https://www.javascript.com/

Exit mobile version