treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Cloning a Node and re-using it.

  • Hello,

    I want to clone a node(p element and it's attributes), delete it, then remake it and use the cloned node for the attributes. I have:
    window.onload = retain; 

    function retain() {
    window.bar = document.getElementById("outer").getElementsByTagName('p')[0];
    window.cbar = bar.cloneNode(true);
    }
    function home() {
    var foo = document.getElementById("outer")

    h3 = document.createElement('h3');
    h3.appendChild(document.createTextNode("John Doe"));
    foo.appendChild(h3);

    p = document.createElement('p');
    p.appendChild(document.createTextNode(cbar));
    foo.appendChild(p);

    }
    But where the P element should show "this is text formated correctly" it shows "[object HTMLParagraphElement]".

    Can anyone help?


  • Try
    p.appendChild(cbar);
    instead of
    p.appendChild(document.createTextNode(cbar));
  • Thanks. It kind of worked. Since cloneNode(true) will also clone the child elements, shouldn't the element P, it's child text nodes, and their attributes be saved?