onKeyUp event calculation not working on the following rows from php generated forms except the first one



PHP Snippet 1:

window.onload=function() {
  document.getElementsByName("qty[]")[0].onkeyup=function() {calc(1)};
  document.getElementsByName("cost[]")[0].onkeyup=function() {calc(1)};
}

PHP Snippet 2:

window.onload = function () {
    var qtyArr = document.getElementsByName("qty[]"),
        costArr = document.getElementsByName("cost[]"),
        length = qtyArr.length,
        i = 0, func = null;

    for (; i < length; i++) {
        func = (function (i) { return function () { calc(i + 1); }; }(i));
        qtyArr[i].onkeyup = func;
        costArr[i].onkeyup = func;
    }
};