By using our mini forum, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy

Q: Sum Numbers with HTML Form and JS

+2 votes
Help me create an HTML form and Sum Numbers with JavaScript code. The script should be in the HTML form itself as script
asked in JavaScript category by user hues
edited by user golearnweb

1 Answer

+1 vote
Best answer

The solution:

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">

    Number 1: <input type="text" name="num1"><br>
    Number 2: <input type="text" name="num2"><br>
    Sum: <input type="text" name="sum"><br>
    <input type="button" value="Sum" onclick="calcSum()">

    function calcSum() {
        let num1 = document.getElementsByName("num1")[0].value;
        let num2 = document.getElementsByName("num2")[0].value;
        let sum = Number(num1) + Number(num2);
        document.getElementsByName("sum")[0].value = sum;



answered by user john7
selected by user golearnweb