I would like to grab the HTML element and all it's computedStyles.
I managed to make it work when the HTML element has no children, but what if the HTML element has many children, how can I grab all the computedStyles of each child
here's what I have so far:
- The user clicks on an html element, I grab the HTML element using event.target.cloneNode(true)
- I then set an Id to the cloned element
- I grab the computedStyle
- Then I add the style to my cloned element
private onClickHtmlCssGrabber = (event) => {
// clone the html element
let clickedHtmlElement = event.target.cloneNode(true)
// set an id to the cloned element
clickedHtmlElement.setAttribute('id', 'clonedElement')
// get the computed style of the element
let clikedHtmlElementStyle = getComputedStyle(event.target).cssText
// set the style on my newly cloned element
clickedHtmlElement.style.cssText = clikedHtmlElementStyle
}
So basically at this point I end up with something like:
<div id="clonedElement" style="... all the coputedStyle">Example text</div>
But my problem occurs when the HTML element that I want to extract has many children:
<div class="style1">
<div class="style-child-1">
<div class="style-child-2">
example text child 2
<div>
<div class="style-child-3">
example text child 3
</div>
<div>
</div>
So how can I extract the whole HTML element with its children and their styles ?