Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript Cloning a Node and re-using it.

  • This topic is empty.
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #38795
    dman777
    Member

    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?

    #105582
    Mottie
    Member

    Try

    p.appendChild(cbar);

    instead of

    p.appendChild(document.createTextNode(cbar));
    #105631
    dman777
    Member

    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?

Viewing 3 posts - 1 through 3 (of 3 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.