magritton
5/17/2017 - 6:20 PM

This copies a row from a table and places it at the bottom. It also changes the name attribute of the copied row's elements.

This copies a row from a table and places it at the bottom. It also changes the name attribute of the copied row's elements.

function cloneRow() {
    var referenceNodes = document.getElementById("DataFieldsFilterTable").getElementsByTagName("tr");
    var referenceNode = referenceNodes[referenceNodes.length - 1];
    var newNode = referenceNode.cloneNode(true);

    $(newNode).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));
        }
    });

    referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
}