[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Cloning element

Posted on 2006-04-03
4
Medium Priority
?
613 Views
Last Modified: 2006-11-18
I would like to copy/clone html element to spare some lines of javascript code and probably performance because I will be using this frequently.
I tried with something simple (div), but can't make it work.
My environment:
 - parent document
 - nested iframe document

Parent document contains: <div id='myDiv' style='display:none;'>this is my div</div>
and javascript:
el = iframeDoc.body.appendChild(document.getElementById("myDiv").cloneNode(false));
el.style.display="inline";

This line of javascript code is called from iframe and doesn't work. It reports "invalid argument" error.
If I runtime create element (createElement()) and append it to the iframe document body then it works. Where's the catch?

Thanks,
Peter
0
Comment
Question by:hpet
  • 2
  • 2
4 Comments
 
LVL 9

Accepted Solution

by:
smaccari earned 200 total points
ID: 16361332
I don't think you can even append a node in your frame document if you created it with document.createElement() (document being the parent document).
You can if you use frameDoc.createElement and then append it to frameDoc.
In fact, a node belongs to a document object and can not be appended to another document object.
0
 
LVL 9

Expert Comment

by:smaccari
ID: 16361385
By the way, looks like Firefox enables this kind of operation, tried your simple code and it works.
0
 
LVL 1

Author Comment

by:hpet
ID: 16361528
I know, but it doesn't work in IE.
It works without problems if I createElement, setAttribute, and then append. but I have some idea in my head that makes me think that cloning an existing node will be faster and easier if I will use this on frequent basis (cloning vs. always newly create and set everything).

Now that you brought up document objects, it makes sence, and not... if I clone node then specification says it is just a copy of object not belonging anywhere until you append it. It should behave the same way as if I manualy create it.

I think cross document object restrictions shouldn't apply here because I am calling this script from the document object I want to append clone to.
0
 
LVL 1

Author Comment

by:hpet
ID: 16362217
I think I have found explanation:
http://neo.dzygn.com/archive/id/56

...and here we are... oh, I just love it.

I think I will stick to createElement, appendElement since it appears to be less a trouble (I don't have that many elements to create anyhow. Cloning will be left for better days.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question