
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
JavaScript Sum function on the click of a button
Let’s say the following is our button −
<button type="button" onclick="addTheValue(10)">Sum </button> </body>
We are calling the function addTheValue(10) with parameter 10 on button click. On clicking the button, we are adding the value 10 as in the below code −
Example
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initialscale= 1.0"> <title>Document</title> <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> <script src="https://code.jquery.com/jquery-1.12.4.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fontawesome/ 4.7.0/css/font-awesome.min.css"> </head> <body> <p> <h1> Adding 10 each time whenever you click the Sum Button......</h1> </p> <b id="firstValue">10</b> <button type="button" onclick="addTheValue(10)">Sum </button> <script> function addTheValue(secondValue) { var fValue = document.getElementById("firstValue"); firstValue.innerHTML = parseInt(fValue.innerHTML) + parseInt(secondValue); } </script> </body> </html>
To run the above program, save the file name “anyName.html(index.html)” and right click on the file. Select the option “Open with Live Server” in VS Code editor.
This will produce the following output −
Now, I am going click the “Sum” button for the first time.
This will produce the following output −
- Related Articles
- Enter a number and write a function that adds the digits together on button click in JavaScript
- How to hide a div in JavaScript on button click?
- Display array items on a div element on click of button using vanilla JavaScript
- How to trigger a button click on keyboard "enter" with JavaScript?
- How to call a JavaScript function on click?
- How to remove li elements on button click in JavaScript?
- Trigger a button click and generate alert on form submission in JavaScript
- How to call a JavaScript function on a click event?
- How to add and remove names on button click with JavaScript?
- How to click on a button with Javascript executor in Selenium with python?
- How to close JFrame on the click of a Button in Java
- Add a pressed effect on button click with CSS
- Multi-selection of Checkboxes on button click in jQuery?
- How to detect click on HTML button through javascript in Android WebView?
- How to display JavaScript array items one at a time in reverse on button click?

Advertisements