Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

selecting an element by a class name with a dot in it with Cheerio

I want to extract information out of an HTML document with Cheerio.
I cannot alter the content of the HTML file as it has an external origin.

The element I want to extract has a class name with a dot in it:

<span class='prop-data.size'> the content </span>

If I use the class name in Cheerio as a selector, I do not get back the element.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

$('.prop-data.size').text() <-- gives null

Is it possible to select an element by a class name with a dot in it in Cheerio?

Can I escape the dot or so?

>Solution :

Yes, escape the .:

const cheerio = require("cheerio"); // ^1.0.0-rc.12

const html = "<span class='prop-data.size'> the content </span>";

const $ = cheerio.load(html);
console.log($(".prop-data\\.size").text()); // => the content
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading