CristianC
CristianC

Reputation: 301

Why is this Cheerio attribute selector not working?

I want to use the following selector normally found in jQuery to create a function that works on its children:

var useroverviewHook = $("div[data-pnref='overview'] *[data-overviewsection]");   

However, it is not working in Cheerio. Nodejs console is reporting

console.log($(useroverviewHook).length) is 0

even though, when testing in the browser, the value is not 0, multiple elements exists.

First I tought the content is dynamic but if I output

res.writeHead(200, {'Content-Type': 'text/plain'}); res.write(html);

data-overviewsection is there. But cheerio says it's not.

Upvotes: 0

Views: 3792

Answers (1)

Alex
Alex

Reputation: 401

First at all, you dont need to use $() twise

var useroverviewHook = $("div[data-pnref='overview'] *[data-overviewsection]");
console.log(useroverviewHook.length)

And you can simplify this selector (remove *):

div[data-pnref='overview'] [data-overviewsection]

Check, maybe you did some mistake when type selectors names and attributes because your syntax is fine.

Also, sometimes browser modify HTML if it was broken when I have similar issues I save response into a file and check response html again.

Upvotes: 1

Related Questions