I've always coded my hide and show jQuery functions in a particular way, but I think that I could write them better.
Below is a filter where I view and hide different images within a portfolio.
HTML
<div class="filters">
<a id="all" class="active"><label>All Projects</label></a>
<a id="branding"><label>Branding</label></a>
<a id="websites"><label>Websites</label></a>
<a id="digital"><label>Digital and Print</label></a>
<a id="motion"><label>Motion Graphics</label></a>
<a id="photography"><label>photography</label></a>
</div>
JQUERY
$(document).ready(function() {
$("#all").click(function() {
$("#all").addClass("active");
$("#branding, #websites, #digital, #motion, #photography").removeClass("active");
$(".all").show();
});
$("#branding").click(function() {
$(".branding").show();
$("#branding").addClass("active");
$("#all, #websites, #digital, #motion, #photography").removeClass("active");
$(".websites, .digital, .motion, .photography").hide();
});
$("#websites").click(function() {
$(".websites").show();
$("#websites").addClass("active");
$("#branding, #all, #digital, #motion, #photography").removeClass("active");
$(".digital, .branding, .motion, .photography").hide();
});
$("#digital").click(function() {
$(".digital").show();
$("#digital").addClass("active");
$("#branding, #websites, #all, #motion, #photography").removeClass("active");
$(".branding, .websites, .motion, .photography").hide();
});
$("#motion").click(function() {
$(".motion").show();
$("#motion").addClass("active");
$("#branding, #websites, #all, #digital, #photography").removeClass("active");
$(".branding, .websites, .photography").hide();
});
$("#photography").click(function() {
$(".photography").show();
$("#photography").addClass("active");
$("#branding, #websites, #all, #digital").removeClass("active");
$(".branding, .websites").hide();
});
});
I reference the code from here code https://codepen.io/3be894d6-0f8e-489a-ae28-476df53bc557/pen/QXdjVJ.