JSFiddle - React, Tailwind, and code Playground

by Uriel Zarnihchi

HTML

Qty1: <input type="checkbox" value="checked" /> checkbox <input type="text" name="qty1" id="qty"/><br>
Qty2: <input type="checkbox"  value="checked" /> checkbox <input type="text" name="qty2" id="qty"/><br>
Qty3: <input type="checkbox"  value="checked" /> checkbox <input type="text" name="qty3" id="qty"/><br>
Qty4: <input type="checkbox"  value="checked" /> checkbox <input type="text" name="qty4" id="qty"/><br>
Qty5: <input type="checkbox"  value="checked" /> checkbox <input type="text" name="qty5" id="qty"/><br>
Qty6: <input type="checkbox"  value="checked" /> checkbox <input type="text" name="qty6" id="qty"/><br>
Qty7: <input type="checkbox"  value="checked" /> checkbox <input type="text" name="qty7" id="qty"/><br>
Qty8: <input type="checkbox"  value="checked" /> checkbox <input type="text" name="qty8" id="qty"/><br>
<br><br>
Total : <input type="text" name="total" id="total"/>

<a href="javascript:sumInputs()">Sum</a>

JavaScript

window.sumInputs = function() {
    var inputs = document.getElementsByTagName('input'),
       
        result = document.getElementById('total'),
        sum = 0;            
    
    for(var i=0; i<inputs.length; i++) {
        var ip = inputs[i];
    
        if (ip.name && ip.value("checked") &&                 ip.name.indexOf("total") < 0 ) {
            sum += parseInt(ip.value) || 0;
        }
    
    }
    
    result.value = sum;
}