Reputation: 633
I am trying to filter some elements based on it's data attributes. Here's the code I've written until now:
$(document).on('change', '.filter-mobile_categories :radio[name="filterSize"]', function(){
const size_value = this.value;
$('.li-product').hide();
if ($('.li-product').dataset.sizes !== "" && $('.li-product').dataset.sizes.indexOf('-' + size_value + '-')){
//refer to those elements
}
});
How do I refer to the elements that comply with the if's condition?
Or using a totally different method like .filter()
would be better? If so, how?
Thanks!
Upvotes: 1
Views: 223
Reputation: 337570
Your question suggesting filter()
is indeed the better way to go here, as it provides you with a jQuery object containing only the relevant elements. Try this:
$(document).on('change', '.filter-mobile_categories :radio[name="filterSize"]', function() {
const size_value = this.value;
let $products = $('.li-product').hide();
let $filtered = $products.filter((i, el) => el.dataset.sizes !== '' && el.dataset.sizes.indexOf(`-${size_value}-`));
// use $filtered here...
});
Upvotes: 2