Cloning a Nodelist in a Bookmarklet

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DP413
    New Member
    • Sep 2006
    • 1

    #1

    Cloning a Nodelist in a Bookmarklet

    Hey guys need your help :)

    I'm trying to clone a nodelist in a bookmarklet so that I can use document.write to output each item in the nodelist to the browser.

    Right now I'm basically grabbing a nodelist, iterating through it and getting stopped after the first document.write, which I think is because the nodelist reference disappears after the document is changed with document.write.


    [PHP]javascript: var someElements = document.getEle mentsByTagName( 'a'); function writeElements(t heElements){for (i = 0; i < theElements.len gth; i++){ document.write( theElements[i].href);}}; writeElements(s omeElements ); [/PHP]

    In non booklet form:
    [PHP]var someElements = document.getEle mentsByTagName( 'a');
    function writeElements(t heElements){
    for(i = 0; i < theElements.len gth; i++){
    document.write( theElements[i].href);
    }
    };
    writeElements(s omeElements); [/PHP]
    That bookmarklet will print out the first link then fail because theElement = nothing when the new page is written.

    I'd really like to know how you can create a hard, deep, complete copy of an array of objects (I think that's what a nodelist is) so that I can write all of a pages links to the browser.

    If there is any need for clarificaiton please let me know, thank you.
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    For a node, you can create a complete deep copy using cloneNode(true) - see link.

    Instead of document.write( ), use DOM methods such as document.create Element(), createTextNode( ), appendChild(), etc. or even innerHTML.

    Comment

    Working...