JQuery for attributes with values ​​greater or less than the sum using> or <

I have several divs as above

<div class="roomnamecta" data-price="1189" data-adult="3">Room 1</div>
<div class="roomnamecta" data-price="578" data-adult="1">Room 2</div>
<div class="roomnamecta" data-price="650" data-adult="2">Room 3</div>

In jQuery, I can present an example div for which data-adult = specific value

// init (first i'm hidding all my divs)
$('.roomnamecta').hide();
// now i'm just showing depending on "data-adult" value
$('.roomnamecta[data-adult="3"]').show();

What I would like to do is something like this

$('.roomnamecta[data-adult>="3"]').show();
// doesn't work

And the best thing I want to do is to do as an example

$('.roomnamecta[data-adult>="3"],.roomnamecta[data-price>="1100"]').show();

How to write such a request, do I need to use an object? as?

+4
source share
1 answer

Since you cannot accomplish this with the attribute selector (for example, you are trying to do), you will have to iterate over the elements and check.

, .filter() , data-adult 3

$('.roomnamecta[data-adult]').filter(function () {
    return $(this).data('adult') >= 3;
}).show();

:

$('.roomnamecta[data-adult], .roomnamecta[data-price]').filter(function () {
    return $(this).data('adult') >= 3 || $(this).data('price') >= 1100;
}).show();
+9

All Articles