NodeJS: innerHTML with xmldom

npm library xmldom doesn’t have something like innerHTML. It has only specific features, not full features of W3C DOM. Here, I introduce how to change inner html text with xmldom.


  • NodeJS 10.9.0
  • npm 6.6.0
  • xmldom 0.1.27
  • Kotlin 1.3.11

How to do

Now, suppose change the internal html of one tag, which is gotten by getElementById or something else. I’ll show the example code first, in Kotlin. Compile it to JavaScript, then it works.

It uses replaceChild to change internal HTML. At the last, I remove xmlns=\"\" from entire html string, because the HTML output from toString method contains the attribute and value.

In my environment, the code is compiled to app.js, and NodeJS code is as follows.

Executing the script, the following message is output.

Split to function

To make it more useful, extract changing inner html part to the function. Below I created the function, changeInnerHtml, for replacing the innerHTML value.