Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert NodeList BACK to HTML

I'm using docx.js, which converts docx to html5 in the browser. The function convertContent outputs a NodeList. The issue is I need an HTML/XML string, not a NodeList.

Is there any way to convert a NodeList back into HTML? There are plenty of examples of going the other way or converting it to an array, but none on how to convert it back to HTML.

like image 325
sinθ Avatar asked Oct 23 '25 18:10

sinθ


1 Answers

I'm a bit unclear on your question (specifically I need HTML5)

If you want the string representation this will create a string of the html for each node in the list

var html = Array.prototype.reduce.call(nodes, function(html, node) {
    return html + ( node.outerHTML || node.nodeValue );
}, "");

Update: fix textnodes showing up as undefined

Try placing this in console on this site

var htmlstr = Array.prototype.reduce.call($("div")[43].childNodes, function(html, node) {
    return html + ( node.outerHTML || node.nodeValue );
}, "");

console.log(htmlstr);
like image 107
megawac Avatar answered Oct 26 '25 07:10

megawac



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!