I’m trying to set up a filter using jQuery. I’ll have a certain amount of divs, each with a numeric value (let’s say price).
The code below works fine, until you enter a value less than 10. Then you get results that shouldnt be there. (enter 4 for example)
Any help on fixing this would be great! thanks!
<script>
function sortmebaby()
{
var divList = $('#containerMonkey div[id^="monkey_"]');
$.each(divList, function(index, value)
{
console.log($(value).attr('xprice'));
if ( $(value).attr('xprice') > $('#mankipower').val())
$(value).hide();
else
$(value).show();
//alert(index + ': ' + value);
});
}
</script>
<div id="containerMonkey">
<div id="monkey_1" xprice="10">10</div>
<div id="monkey_2" xprice="20">20</div>
<div id="monkey_3" xprice="30">30</div>
<div id="monkey_4" xprice="40">40</div>
<div id="monkey_5" xprice="50">50</div>
</div>
<input type="text" name="mankipower" id="mankipower">
<input type="button" value="PUSH" onclick="sortmebaby()">
Thanks!
The condition in your
ifstatement is comparing strings, not integers as you’re expecting. You can useparseIntto convert the strings to numbers:Here’s an updated example.