To add two numbers in JavaScript dynamically use onblur Event to call again addition function. Actually, onblur Event Execute a JavaScript when a user leaves an input field:
How to add two numbers in JavaScript dynamically Example.
HTML example code, Automatic both filed will add value when use leave input field.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<!DOCTYPE html> <html> <body> <input type="text" id="Num1" value="1" onblur="reSum();"/> <input type="text" id="Num2" value="1" onblur="reSum();"/> <br> <p>Auto Sum</p> <input type="text" id="Sum" value=""/> <script> function reSum() { var num1 = parseInt(document.getElementById("Num1").value); var num2 = parseInt(document.getElementById("Num2").value); document.getElementById("Sum").value = num1 + num2; } </script> </body> </html> |
Output:

Do comment if you have any doubts an suggestion on this topic.
Note: All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version