Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clone a whole table in Javascript?

I tried to use cloneNode mentionned here Copy the content of one table into another but Chrome says cloneNode is not a function

https://jsfiddle.net/4wczdykc/1/

<table>
    <thead>

        <tr>
            <th scope="col" colspan="1">TABLE TO CLONE</th>
        </tr>

        <tr>
            <th>Column</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td></td>                         
        </tr>
    </tbody>
</table>

script:

myTable = document.getElementsByTagName("Table")[0];
myClone = myTable.cloneNode(true);
document.body.appendChild(myClone);
like image 729
user310291 Avatar asked Jul 17 '26 01:07

user310291


1 Answers

The getElementsByTagName() method accesses all elements with the specified tagname.So you have to select the first element of the NodeList. So passed [0] to select it.

    myTable = document.getElementsByTagName("table")[0];
    myClone = myTable.cloneNode(true);
    document.body.appendChild(myClone);

WORKING FIDDLE

like image 194
brk Avatar answered Jul 19 '26 14:07

brk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!