javascriptbuttonduplicatesclonenode

How can I duplicate a button inclusive its functions with JavaScript?


I have a button which appends some elements. I want a button which duplicates this button.

I found some articles about clone(), but I believe that's jQuery?

I can't seem to find the answer but I thought something like this:

var copybutton = create("input");
    copybutton.type = "button";
    copybutton.id = "copybutton" + counter;
    copybutton.value = "copybutton";
    addEvent(copybutton, "click", duplicatefunction);

function duplicatefunction()
{
var duplicatebutton = appendbutton.cloneNode (true);
}

Well at least that would make sense to me a little, but of course it doesn't work. How can I do this?


Solution

  • Given:

    <div id="original">
       <p>Hello World</p>
    </div>
    

    You can use something like:

    var e1 = document.getElementById("original"), e2;
    e2 = e1.cloneNode(true);
    

    Set the parameter to cloneNode true if you want to recursively copy the node and its children.