I have two drop down menus (min age and max age). I can filter on both them but individually but what I want to be able to do is only show the divs whose data-age attribute is equal or within the range of both values (21 to 26 for example would return 22 etc but not 20 or 27).
Apologies I should have explained in my original post - the fields are filtering a bunch of divs on the same page that contain a matching attribute - in this case data-age the location field I am using to filter based on location looks for data-town.
this.value will be an integer, how will that ever equal a string "all"?
given you want all the values in a given range, seems you need a loop. I experimented here, see what you think of it, i placed the trigger event on button, i dislike the change event for this. You can easily change it back
Just updated the code slightly to include the all in the data attributes.
All is also set in the divs data attributes so that all div elements are shown if the user selects the default value again after having filtered the divs before. The age fields used to have âSelect min ageâ as a default option.
I have other filters - location for example - and i had an issue where it wasnât possible to get back to all results so built that in.
Not in relation to the age filters - I should have removed the all conditions. It was left over fro the location filter I also have set up, for clarity Iâve copied that code below too.
<select id="town-select" class="soflow">
<option value="all">Select your location...</option>
<?php foreach(get_alllocations() as $locationlist): ?>
<option value="<?php echo htmlentities($locationlist['city'], ENT_QUOTES, 'UTF-8'); ?>"><?php echo htmlentities($locationlist['city'], ENT_QUOTES, 'UTF-8'); ?></option>
<?php endforeach; ?>
</select>
<script type="text/javascript">
$("#town-select").change(function(){
if (this.value === 'all') {
$("div[data-town]").show();
} else {
$("div[data-town]").hide();
$("div[data-town=" + this.value +"]").show();
}
});
</script>
<div class="box" data-town="all Manchester" data-age="20">
Div content here
</div>
<div class="box" data-town="all Birmingham" data-age="25">
Div content here
</div>
Yes, it did work. If for some reason the default value was selected again - all the divs would display until another value was selected.
Yes, thanks, that was the logic I was looking for. I could get it to display results that matched exactly the age but not ones included between the two values.
The plan is chain the filters eventually so that if a location is selected, the age range is only taken from the already filtered divs. But that is for another day
@stetim94 thinking about a little - I have decided to go with a button rather than the change event but is there a quick and easy function that can clear the filters (like a reset button) that I can include too.