Skip to content

Instantly share code, notes, and snippets.

@BeFiveINFO
Last active November 18, 2019 00:10
Show Gist options
  • Save BeFiveINFO/f711e36dedf3f27bc6f82d7865a521a0 to your computer and use it in GitHub Desktop.
Save BeFiveINFO/f711e36dedf3f27bc6f82d7865a521a0 to your computer and use it in GitHub Desktop.
JSDOM appendChild sample
const $Fs = require('fs');
const { JSDOM } = require('jsdom'); // uninstall when done
const htmlText = `
<div id="target">
<p>test</p>
</div>
`;
const _htmlTextDom = new JSDOM(htmlText);
const _htmlTextDomDocument = _htmlTextDom.window.document;
const target = _htmlTextDomDocument.querySelector('div#target');
const div = _htmlTextDomDocument.createElement("div");
div.innerHTML = "<p>Hello</p>";
target.appendChild(div);
console.log(_htmlTextDomDocument.body.innerHTML);
/* Expected output
<div id="target">
<p>test</p>
<div>
<p>Hello</p>
</div>
</div>
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment