What's wrong with filtering by price?

Hello, there are 2 input and button.
If you enter a minimum price, below what must be hidden. Similarly with max, only hides the fact that the above
<input style="border: 1px solid silver;border-radius: 5px;" class="uk-input price_min" id="form-horizontal-text" type="text" placeholder="From">
<input style="border: 1px solid silver;border-radius: 5px;" class="uk-input price_max" id="form-horizontal-text" type="text" placeholder="To">
<button class="uk-button uk-button-danger uk-width-1-1 price_min_max_btn">Apply</button>

There is such code, but it filters out crooked.
The price of the goods in the unit with product, is in the attribute data-price
Class of goods item_block_filter
$('.price_min_max_btn').click(function() {
 min_price = $('.price_min').val();
 $('.item_block_filter').each(function() {
 if ( $(this).attr("data-price") <= min_price ) {
console.log(this);
console.log(min_price);
$(this).hide();
}
});
 max_price = $('.price_max').val();
 $('.item_block_filter').each(function() {
 if ( $(this).attr("data-price") >= max_price ) {
console.log(this);
console.log(max_price);
$(this).hide();
}
});
 });


Please help to understand code
April 7th 20 at 15:46
1 answer
April 7th 20 at 15:48
Solution
Compare strings instead of numbers. Hide elements but not display them.

$('.price_min_max_btn').click(function() {
const
 min = $('.price_min').val(),
 max = $('.price_max').val();

 $('.item_block_filter').each(function() {
 const price = +this.dataset.price;
 $(this).toggle((!min || min <= price) && (!max || price <= max));
});
});
Oh, I was far from resolved(
Thank you very much! - giovanny_Schmitt commented on April 7th 20 at 15:51

Find more questions by tags jQuery