Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I programmatically copy all of the style attributes from one DOM element to another

I have a page with two frames, and I need to (via javascript) copy an element and all of its nested elements (it's a ul/li tree) and most importantly it's style from one frame to the other.

I get all the content via assigning innerhtml, and I am able to position the new element in the second frame with dest.style.left and dest.style.top and it works. But I'm trying to get all the style information and nothing's happening.

I'm using getComputedStyle to get the final style for each source element as I loop through each node then and assigning them to the same position in the destination nodelist and nothing happens to visually change the style.

What am I missing?

like image 606
stu Avatar asked Apr 26 '10 17:04

stu


People also ask

How do I copy a whole DOM?

You can easily change the DOM without having to edit the HTML as a big piece of string. Right click on a node and select Copy. You can paste in your code editor, or for prototyping, you can paste the DOM node elsewhere in the DOM tree.

How do you grab elements from DOM?

One of the most common methods to access an element in HTML DOM is getElementById() which accesses an element based on the value of its ID attribute. The value of the ID attributes are supposed to be unique and no two elements on a single HTML page should have similar IDs.

How can we fetch all attributes for an HTML element?

To get all of the attributes of a DOM element: Use the getAttributeNames() method to get an array of the element's attribute names. Use the reduce() method to iterate over the array. On each iteration, add a new key/value pair containing the name and value of the attribute.

What is generally the fastest method to retrieve an element from the DOM?

Accessing Elements by ID The easiest way to access a single element in the DOM is by its unique ID. You can get an element by ID with the getElementById() method of the document object.


1 Answers

Well I gave up the original tack of trying to get the [computed] style information out of the source tag, I just never got it to work.

So instead I tried this, and it did work...

First I grabbed the -style- tag from the source frame, then I appendChilded it to the end of the -head- tag of the second frame. For which this proved useful... How do you copy an inline style element in IE?

Then I made a few nested div elements, each having an id or style class I needed to inherit so that the hierarchy matched the first frame. Then I shoved the innerhtml of the source frame's tag that I wanted to copy and then finally appendChilded it to the body of the second frame, and magically, it all worked.

var topd = doc.createElement('div'); // make a div that we can attach all our styles to so they'll be inherited
topd.id = 'menuanchorelement';
// shove our desired tag in the middle of a few nested elements that have all the classes we need to inherit...
topd.innerHTML = "<div id='multi-level'><ul class='menu'>" + dh.innerHTML + "</ul></div>";

doc.body.appendChild(topd); // voila

like image 142
stu Avatar answered Oct 23 '22 18:10

stu