cheerio-eq
v1.0.1
Published
Add :eq() selector functionality to cheerio
Downloads
163
Readme
cheerio-eq
Add :eq()
selector functionality to cheerio.
If you are looking for a more advanced solution with support for multiple advanved selectors, check out the cheerio-advanced-selectors module.
What's the problem?
Cheerio sacrifices advanced CSS selector support for speed. This means
for instance that the :eq()
selector isn't supported. The work-around
is normally to use the .eq()
function instead:
// this will not work:
$('div:eq(1)');
// use this instead:
$('div').eq(1);
This is a good alternative if you write the CSS selectors from scrach,
but what if you are working with selectors that already contain :eq()
?
Don't fear, cheerio-eq is here :)
Solution
The solution to the problem is to automatically parse the selector
string at run-time. So if you give cheerio-eq a selector like
div:eq(1)
it will return the following cheerio cursor:
$('div').eq(1)
.
It also works for complex selectors, so that div:eq(1) h2:eq(0) span
will be converted and interpreted as
$('div').eq(1).find('h2').eq(0).find('span')
.
Installation
npm install cheerio-eq
Usage
var cheerio = require('cheerio');
var find = require('cheerio-eq');
var html = '<div>foo</div><div>bar</div>';
var selector = 'div:eq(1)';
var $ = cheerio.load(html);
console.log(find($, selector).text()); // => 'bar'
API
The cheerio-eq module exposes a single function, which takes 2 arguments:
- The cheerio DOM object (usually just named
$
) - A string containing the selector to parse
License
MIT