Insert table row after clicked

by cheakeanauva

HTML

<table>
    <body>
        <tr>
            <div id=Data>asdfasdfa</div>
            <td><button onclick="addRow(this);">Add row</button></td>
        </tr>
        <tr>
            <td>Second row</td>
            <td><button onclick="addRow(this);">Add row</button></td>
        </tr>
    </tbody>
</table>

JavaScript

function addRow(button) {
    var buttonTd = button.parentNode;
    var rowTr = buttonTd.parentNode;
    var tableTbody = rowTr.parentNode;
    var tableTable = tableTbody.parentNode;
    
    var newTr = rowTr.cloneNode(true);
    var newTrNameTd = newTr.children[0];
    var oldName = newTrNameTd.innerHTML;
    var newName = oldName + ' new';
    newTrNameTd.innerHTML = newName;
    
    var nextRow = rowTr.nextSibling;
    if (nextRow) {
        tableTbody.insertBefore(newTr, nextRow);
    } else {
        tableTbody.appendChild(newTr);
    }
}