Search code examples
javascripthtml-tablerowcloneaddition

appendTo doesn't take my ID


I am not so good at JS, but I am trying to add dynamically a row with content (clone). This is working, IF I put

        var apcount = 0;

        function add(id, apmax) {
        var aptable = id;
            if (apmax > apcount) {
                $("table tr:last").clone().appendTo("#m1s1t2").find(':input').attr('name', function(index, name) {
                    return name.replace(/(\d+)$/, function(fullMatch, n) {
                        return Number(n) + 1;
                    });
                })
            }; apcount++;
        }

the 'id' direct into '.appendTo'. The call of this function is a button

<input onclick="add('m1s1t2',2)" type="button">

Actually I want to have something like '.appendTo(id)'

Anyone an idea?


Solution

  • So use the id variable you pass in

    change

    .appendTo("#m1s1t2")
    

    to

    .appendTo("#" + id)