I have written a post recently on another node.js solution for this sort of thing called html-to-json, however in this post I will be writing about cheerio. Which is a server side implementation of core jQuery.
So I just need to do the usual npm install with the package name cheerio in a test folder.
And my basic basic.js file looks like this:
So that when I call my basic.js file from the command line I get the innerText of the paragraph element.
Selectors work in very much the same way as in jQuery. The ushual selection by tag, class, and id work in very much the same fashion.
There are many methods that can be used with a collection, picking one at random there is hasClass that can be used to find if a collection is using a certain class or not.
A method that I actualy use often is for course the each method that loops over all elements in a collection.
The method that I pass to each will give me each index, and a reference to the corresponding element in a collection.