Cloning element

Posted on 2006-04-03
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));"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?

Question by:hpet
    LVL 9

    Accepted Solution

    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.
    LVL 9

    Expert Comment

    By the way, looks like Firefox enables this kind of operation, tried your simple code and it works.
    LVL 1

    Author Comment

    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.
    LVL 1

    Author Comment

    I think I have found explanation:

    ...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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Android App Development

    This course will involve creating widgets, customize list view, grid view, spinners, etc. Creating applications using audio, video, and SQLite database. Ending with publication on Google Play.

    This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
    The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
    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…

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now