max li
max li

Reputation: 2457

Cheerio does not come with all selector

While jQuery comes with a very useful all-selector http://api.jquery.com/all-selector/ What I want to do is find selectors that contains css position absolute, this can be easily archived by doing:

$('*').filter(function(){
  return $(this).css('position') == "absolute";
});

so the question is there a similar solution for cheerio?

Upvotes: 0

Views: 1415

Answers (2)

adeneo
adeneo

Reputation: 318162

The asterisk works fine, try it like this

var html  = '<div class="test" style="position:absolute">test</div>';
    html += '<div class="test" style="position:relative">test2</div>';

var $ = cheerio.load(html);

var elems = $('*').filter(function(){
                return $(this).css('position') == "absolute";
            });

console.log( elems.html() ); // returns just "test"

note that you have to return the result, Cheerio is not jQuery, and there is no DOM, so any modification has to be returned to a new variable.

Upvotes: 1

Sadikhasan
Sadikhasan

Reputation: 18600

$(".test").each(function(){
   //You will get all elements contains test class
});

Upvotes: 0

Related Questions