settingsAccountsettings
Menusettings

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">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>

<form>
    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()">
</form>

<script>
    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;
    }
</script>


</body>
</html>

 

answered by user john7
selected by user golearnweb
...