In these types of situations I do not always need to visualize a browser environment in a nodejs environment in order to do whatever it is that I need to do with this sever side html code. I might just want to
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 functionality, but I can not do everything that I can do in a browser environment with cheerio so it does have its limits.
So I just need to do the usual npm install with the package name cheerio in a test folder as a way of getting started with a simple example of cheerio.
Once i have my basic test folder set up I can work out basic.js file the just severs as a basic starting point for using this package:
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 usual 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 actually use often is the each method that loops over all elements in a collection, real handy for doing this like tabulating values of elements or something to that effect.
The method that I pass to each will give me each index, and a reference to the corresponding element in a collection.