jQuery – Use multiple data search selector

I have some checkboxes, and I want to filter all my DOM elements according to these filters.

My HTML is like :

<div data-year="2018">....</div>
<div data-year="2018">...</div>
<div data-year="2019">...</div>

When I click my checkboxes (I can have several checkboxes checked), I build a string like :

var search = '[data-years="2018"], [data-years="2019"], [data-years="2021"]';

My question is : how can I select all my div in jQuery ?

I tried something like :

var $div = $('div').find(search);

But no success. How can I select all years checked ?

159 thoughts on “jQuery – Use multiple data search selector”

Leave a Comment