jQuery - cloned HTML element by Norlihazmey Ghazali

Cloned HTML element on button click with jquery

by Norlihazmey Ghazali

HTML

<table>
    <tr>
        <td data-unit="spear" class="tooltip">
            <input type="text" size="2" name="spear" />
        </td>
        <td data-unit="sword" class="tooltip">
            <input type="text" size="2" name="sword" />
            <input type="checkbox" name="knife" />
            <select name="book">
                <option>as</option>
            </select>
        </td>
    </tr>
</table>
<button>click</button>

JavaScript

$(document).ready(function () {
    var a = 0;
    $('button').click(function () {
        a++;
        var tr = $('tr').first().clone();
        $(tr).find('input, select').attr('name', function(i, e){
            return e+'-' + a;
        });
        $(tr).appendTo('table');
    })
});