Categories
JavaScript Answers

How to use the DOMParser with Node.js?

Spread the love

Sometimes, we want to use the DOMParser with Node.js.

In this article, we’ll look at how to use the DOMParser with Node.js.

How to use the DOMParser with Node.js?

To use the DOMParser with Node.js, we can use the Cheerio library.

We install it by running

npm i cheerio

Then, we write

const cheerio = require('cheerio');

const $ = cheerio.load(`<!DOCTYPE html><p>Hello world</p>`);
$('p').text('Bye moon');
$.html();

to call cheerio.load with an HTML string to load the HTML string into the $ DOM object.

Then we select p elements and call text to set the text of the p element.

And we call $.html to return the HTML of the manipulate document.

Conclusion

To use the DOMParser with Node.js, we can use the Cheerio library.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *