$(function() {
$('#cityselector').change(function(){
$('.city').hide();
$('#' + $(this).val()).show();
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="row">
<Select id="cityselector">
<option selected value="All">All</option>
<option value="Pune">Pune</option>
<option value="Chennai">Chennai</option>
<option value="Bengaluru">Bengaluru</option>
</Select>
</div>
<div class="row">
<div class="col-md-4 city" id="Pune">
<div class="card">
<div class="card-image">
</div>
<div class="card-content">
<span class="card-title">Pune- Koregaon Park</span>
</div>
</div>
</div>
<div class="col-md-4 city" id="Pune">
<div class="card">
<div class="card-image">
</div>
<div class="card-content">
<span class="card-title">Pune- Vimannagar</span>
</div>
</div>
</div>
<div class="col-md-4 city" id="Chennai">
<div class="card">
<div class="card-image">
</div>
<div class="card-content">
<span class="card-title">Chennai</span>
</div>
</div>
</div>
<div class="col-md-4 city" id="Bengaluru">
<div class="card">
<div class="card-image">
</div>
<div class="card-content">
<span class="card-title">Bengaluru</span>
</div>
</div>
</div>
</div>
I am building a new page called stores on shopify frontend. But, I am not going to use any database for that, I just wanted to filter or show/hide cards based on city selection from dropdown. I have already filter cities using jquery show hide.But it’s not working when there are multiple cards with same city name. It’s showing only one single city card using that. Also, I need to show all city cards, when user does select All option from dropdown. Kindly help me out with that.
2
Answers
You can just check for a value
All
to show all.city
elements, just like this:EDIT:
I just noticed that it’s not the only problem you had, to solve the same city’s names problem, I would work with
classes
instead of working withIDs
.The reason this problem takes place, is because
jQuery
selectors byID
will always return one element, even if there are multiple elements with the sameID
Insted of id, give class names: