For homework in Automata, using DOMParser, we need to format a single string of HTML
<div class="the-best-css-class-like-ever"><div class="youtube-embed" data-oembed="{'version': '1.0', 'type': 'video', 'title': 'Amazing Nintendo Facts', 'html': '<object width=\'425\'><param name=\'movie\' value=\'http://www.youtube.com/v/M3r2XDceM6A&fs=1\'></param>}"><img src="https://www.youtube.com/yt/brand/media/image/YouTube-logo-full_color.png"><img src="https://www.youtube.com/yt/brand/media/image/YouTube-logo-full_color.png"></div><!-- asdf <img> -> --><p>Automata Rules!</p></div>
into a "tabbed", multi line HTML string
<div class="the-best-css-class-like-ever">
<div class="youtube-embed" data-oembed="{'version': '1.0', 'type': 'video', 'title': 'Amazing Nintendo Facts', 'html': '<object width=\'425\'><param name=\'movie\' value=\'http://www.youtube.com/v/M3r2XDceM6A&fs=1\'></param>}">
<img src="https://www.youtube.com/yt/brand/media/image/YouTube-logo-full_color.png">
<img src="https://www.youtube.com/yt/brand/media/image/YouTube-logo-full_color.png">
</div>
<!-- asdf <img> -> -->
<p>
Automata Rules!
</p>
</div>
I have never used Javascript, so how can I use DOMParser to achieve this task?
From what I understand, DOMParser takes HTML and formats it into a tree structure with child elements. However, I've tried to step through the tree, but all I get are null
and undefined
values
[EDIT] Somebody in class gave me a hint to use
var parser = new DOMParser();
var htmlDoc = parser.parseFromString(text, "text/html");
var elements = htmlDoc.body.childNodes;
[EDIT 2] I solved this by stepping down the DOM tree and getting the HTML, for that particular tag, from the nodes by taking the outerHTML values and removing the childrens' values.
element.outerHTML.replace(child.outerHTML, "");
I couldn't find another, easier way to do this. Alan's answer helped greatly, especially the Firefox dev console and debugger.