magritton
5/17/2017 - 6:30 PM

This inserts a row into a table and uses a random number for the row contents names

This inserts a row into a table and uses a random number for the row contents names

function insertRow(el) {
    while (el.parentNode && el.tagName.toLowerCase() !== 'tr') {
        el = el.parentNode;
    }
    if (el.parentNode) {
        var parent = el.parentNode;
        var newRow = el.cloneNode(true);

        $(newRow).find('td').each(function () {
            var el = $(this).find(':first-child');
            var name = el.attr('name') || null;
            if (name) {
                var i = name.substr(name.length - 1);
                var prefix = name.substr(0, (name.length - 1));
                //el.attr('id', prefix + (+i + 1));
                el.attr('name', prefix + (+i + (1 * getRandomInt(0,5000))));
            }
        });

        if (parent.lastChild === el) {
            // add the newElement after the target element.
            parent.appendChild(newRow);
        } else {
            // else the target has siblings, insert the new element between the target and it's next sibling.
            //alert(parent.tagName);
            $(newRow).insertAfter(el);
            //parent.parentNode.insertBefore(newRow, el.nextSibling);
        }
    }

}