How to add two numbers in JavaScript dynamically | Example code

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.

<!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:

How to add two numbers in JavaScript dynamically

Do comment if you have any doubts an suggestion on this topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.