How to copy a cloned node in an iframe to its parent with styles?

Brian

I'm trying to clone a <div> in an <iframe>, and append the cloned div to the parent DOM. I keep the div as display: none to start (when in the iframe), and I make it visible when I clone it in the parent. This all works fine, and here's a minimal snippet:

The parent (top.html):

<iframe src=foo.html></iframe>

The iframe (foo.html)

<html>
<HEAD>
    <style>
        #myid {
            display: none;
        }
    </style>
</HEAD>

<body onload="onload()">
    <script>
    function onload() {
        var div = document.getElementById("myid");
        var newdiv = div.cloneNode(true);
        var body = parent.document.getElementsByTagName("body")[0];
        newdiv.id = "new" + div.id;
        newdiv.style.display = "block";
        newdiv.style.position = "absolute";
        newdiv.style.top = "100px";
        newdiv.style.left = "100px";
        newdiv.style.width = "100px";
        newdiv.style.height = "100px";
        newdiv.style.background = "red";
        body.appendChild(newdiv);
    };
    </script>
    <div id=myid>
        <p>foo
    </div>
</body></html>

My issue that I would like to use internal css to define #myid WHEN THE DIV IS CLONED INTO THE PARENT. But, once the the div is cloned, it only references the CSS in the parent... I'm not (readily) able to modify the CSS of the parent.

Can I make the internal CSS "stick" to the div when it gets cloned?

For example, if I delete the line above:

newdiv.style.background = "red";

And instead add to the internal CSS:

background: red;

The red doesn't stay once I clone the div to the parent.

My only other solution is to just do it all inline, by changing the div, like:

<div id=myid style="background: red;">

And that works, its just that I have a lot of CSS and it's a mess to maintain that way.

Brian

My solution to this (from @charlietfl's suggestion) was to add:

newdiv.style.cssText = window.getComputedStyle(div).cssText;

to copy the computed CSS to the cloned div. Now I can add background: red to the internal CSS, and it is "stuck" to the clone.

The final solution is below:

<html>
<HEAD>
    <style>
        #myid {
            /* Added style here */
            background: red;
            display: none;
        }
    </style>
</HEAD>

<body onload="onload()">
    <script>
    function onload() {
        var div = document.getElementById("myid");
        var newdiv = div.cloneNode(true);
        var body = parent.document.getElementsByTagName("body")[0];
        /* Copy styles here */
        newdiv.style.cssText = window.getComputedStyle(div).cssText;
        newdiv.id = "new" + div.id;
        newdiv.style.display = "block";
        newdiv.style.position = "absolute";
        newdiv.style.top = "100px";
        newdiv.style.left = "100px";
        newdiv.style.width = "100px";
        newdiv.style.height = "100px";
        body.appendChild(newdiv);
    };
    </script>
    <div id=myid>
        <p>foo
    </div>
</body></html>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to just scroll the iframe but not its parent?

From Dev

How to make a div and its children to ignore its parent styles?

From Dev

How do I make CSS styles dependant on its parent element

From Dev

How to make width and height of iframe same as its parent div?

From Dev

How can an iframe get its parent background color?

From Dev

how to delete a node in a linked list without accessing its parent node?

From Dev

JQuery Get Parent URL in IFrame:iframe and its parent are in Different domain

From Dev

how Xcode automatically remove skspritenode form its parent's node

From Dev

How to auto resize the parent node according to its children

From Dev

How to get a piece of an json object with its parent node?

From Dev

How select parent of node in any level with its attribute

From Dev

How to move a node element to be the first inside its parent element

From Dev

Accessing a tag from an iFrame to its parent

From Dev

Copy node and alter its content

From Dev

parent node losing its child

From Dev

Resize iframe and its parent height according to content inside iframe

From Dev

Linux recursive copy files to its parent folder

From Dev

smartgit - how to view and copy to clipboard the repository url for cloned repo?

From Dev

How to lock the position of a cytoscape.js node within its parent node

From Dev

How can I copy the contents of a child folder to its parent folder using command line?

From Dev

How to copy parent node based on child node's condition using XSLT?

From Dev

B-Tree Node References its "Parent"

From Dev

How disqus able inherit parent's styles?

From Dev

How do I associate a parent node with all of its children in an event listener? jQuery / JavaScript

From Dev

How do I get the dom node of a child element in a component without adding logic to its parent/children?

From Dev

How do I get the dom node of a child element in a component without adding logic to its parent/children?

From Dev

How to get an iframe to hold its position?

From Dev

Copying a Child Node into its Parent's Sibling's Child Node

From Dev

Grunt.js: copy file without its parent directory

Related Related

  1. 1

    How to just scroll the iframe but not its parent?

  2. 2

    How to make a div and its children to ignore its parent styles?

  3. 3

    How do I make CSS styles dependant on its parent element

  4. 4

    How to make width and height of iframe same as its parent div?

  5. 5

    How can an iframe get its parent background color?

  6. 6

    how to delete a node in a linked list without accessing its parent node?

  7. 7

    JQuery Get Parent URL in IFrame:iframe and its parent are in Different domain

  8. 8

    how Xcode automatically remove skspritenode form its parent's node

  9. 9

    How to auto resize the parent node according to its children

  10. 10

    How to get a piece of an json object with its parent node?

  11. 11

    How select parent of node in any level with its attribute

  12. 12

    How to move a node element to be the first inside its parent element

  13. 13

    Accessing a tag from an iFrame to its parent

  14. 14

    Copy node and alter its content

  15. 15

    parent node losing its child

  16. 16

    Resize iframe and its parent height according to content inside iframe

  17. 17

    Linux recursive copy files to its parent folder

  18. 18

    smartgit - how to view and copy to clipboard the repository url for cloned repo?

  19. 19

    How to lock the position of a cytoscape.js node within its parent node

  20. 20

    How can I copy the contents of a child folder to its parent folder using command line?

  21. 21

    How to copy parent node based on child node's condition using XSLT?

  22. 22

    B-Tree Node References its "Parent"

  23. 23

    How disqus able inherit parent's styles?

  24. 24

    How do I associate a parent node with all of its children in an event listener? jQuery / JavaScript

  25. 25

    How do I get the dom node of a child element in a component without adding logic to its parent/children?

  26. 26

    How do I get the dom node of a child element in a component without adding logic to its parent/children?

  27. 27

    How to get an iframe to hold its position?

  28. 28

    Copying a Child Node into its Parent's Sibling's Child Node

  29. 29

    Grunt.js: copy file without its parent directory

HotTag

Archive