javascript - Check if number is between 2 values -
i building filter based on div class's , contents.
i wondering if possible pass string follows function: "£0.01 - £100.01
"
and have function show div's html of div between range
so have div class of "price" , contents were: £10.30
from running function , passing string of "£0.01 - £100.01
" it hide div's similar how have done in js below show div's div class "price"'s contents within selected price range.
i have managed similar brand filter provide here:
function brand(string){ var brand = string; $('.section-link').hide(); $('.section-link').children('.brand.' + brand).parent().show(); if (brand == "all brands"){ $('.section-link').show(); } }
any general advice or code appreciated achieve :)
thanks, simon
edit:
target div example:
<div class="section-link"> <div class="price"> £56.99</div> </div>
reply's helping lot, filter function looks awesome pointing out.
i trying find way split initial string being past in, 2 values 1 low , 1 high stripping £ signs
edit:
managed split original string:
var range = string.replace(/\u00a3/g, ''); var rangearray = range.split("-"); alert(rangearray[0]); alert(rangearray[1]);
final edit:
from reply's have kind of been able make function, not entirely working :) can spot have done wrong?
function price(string){ $('.section-link').hide(); var range = string.replace(/\u00a3/g, ''); var rangearray = range.split("-"); low = rangearray[0]; high = rangearray[1]; $('.section-link').children('.price').each(function() { var divprice = $(this).text().replace(/\u00a3/g, ''); if (low <= divprice && high >= divprice){ $(this).parent().show(); } }) }
okay working, had spaces in string. final function (although messy :p) is:
function price(string){ $('.section-link').hide(); var range = string.replace(/\u00a3/g, ''); var rangearray = range.split("-"); low = rangearray[0].tostring(); high = rangearray[1].tostring(); lowmain = low.replace(/ /g,''); highmain = high.replace(/ /g,''); $('.section-link').children('.price').each( function() { var divprice = $(this).text().replace(/\u00a3/g, ''); var maindivprice = divprice.replace(/ /g,''); if (lowmain <= maindivprice && highmain >= divprice){ $(this).parent().show(); } }) }
i'd use function one, range
string gave
function highlightdivs(range) { var lower = range.split(" ")[0].slice(1); var upper = range.split(" ")[2].slice(1); $('.section-link').hide(); $('.section-link').children('.price').each(function() { if (lower <= $(this).val() && upper >= $(this).val()){ $(this).parent().show(); } }); }
Comments
Post a Comment