I am trying to extract Class and IDs from a HTML document. I want result showing in <textarea class="output"></textarea>
when clicking on SUBMIT button.
HTML:
<div id="fullwidth">
<div id="wrapper">
<div id="content">
<div class="fifty">
<textarea class="input">
<div class="font step_footer">
<div class="container">
<div class="font-in">
<ul class="d-flex flex-wrap">
<li class="col-md-3 col-4">
<div class="font-list">
<p>Notre équipeM<br> reconnus<br>obtient<br>istance</p>
</div>
</li>
</ul>
</div>
</div>
</div>
</textarea>
<div id="button-div">
<button id="button">Submit</button>
</div>
</div>
<div class="fifty">
<textarea class="output"></textarea>
</div>
</div>
</div>
</div>
javascript:
const main = () => {
console.log(emptyInnerHTML(document.body))
}
const emptyInnerHTML = (element) => {
return renderHTML(describeHTML(element)).innerHTML
}
const describeHTML = (node) => {
return ({
tagName: node.tagName,
id: node.id || undefined,
classList: [...node.classList],
children: [...node.childNodes]
.filter(child => child.nodeType != Node.TEXT_NODE && child.tagName !== 'SCRIPT')
.map(child => describeHTML(child))
})
}
const renderHTML = (tree) => {
let node = document.createElement(tree.tagName)
if (tree.id) node.setAttribute('id', tree.id)
if (tree.classList && tree.classList.length) node.className = tree.classList.join(' ')
if (tree.children && tree.children.length) {
tree.children.forEach(child => node.appendChild(renderHTML(child)))
}
return node
}
main()
Result is coming when check on Codepen Console tab. I created a Codepen page for it. You can see it in this Codepen link:
https://codepen.io/coderco/pen/BajJMav
I want to make it as result coming when I click on SUBMIT button.
2
Answers
Working Code !!
Observe inside the right output
textarea
, on clicking thesubmit
button, you will be able to see the same result which were you see on load currently.