By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,568 Members | 1,196 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,568 IT Pros & Developers. It's quick & easy.

Cloning a Nodelist in a Bookmarklet

P: 1
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.getElementsByTagName('a'); function writeElements(theElements){for(i = 0; i < theElements.length; i++){ document.write(theElements[i].href);}}; writeElements(someElements ); [/PHP]

In non booklet form:
[PHP]var someElements = document.getElementsByTagName('a');
function writeElements(theElements){
for(i = 0; i < theElements.length; i++){
writeElements(someElements); [/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.
Sep 9 '06 #1
Share this Question
Share on Google+
1 Reply

Expert Mod 15k+
P: 16,027
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.createElement(), createTextNode(), appendChild(), etc. or even innerHTML.
May 14 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.