Search and Filter Gallery

A gallery with keyword title search and filter terms

by Ethan Webster

HTML

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://jsfiddle.net/ewebster/a81v1q46/35/href=%22https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css">
      <form>
      <input  type="text" id="searchEntry" placeholder="Search">
      <input type="submit" id="searchBtn" class="searchButton"/>
      </form>
			<p id="result"></p>
      <p id="test"></p>
			<ul>
		<li>	<button class="filter-button" data-filter="all">All </button></li>
			<li><button class="filter-button" data-filter="short">Short </button></li>
		<li>	<button class="filter-button" data-filter="tall">Tall </button></li>
			<li><button class="filter-button" data-filter="big">Big </button></li>
		<li>	<button class="filter-button" data-filter="small">Small </button></li>
		<li>	<button class="filter-button" data-filter="many">Many</button> </li>
    <li><button class="filter-button" data-filter="few">Few </button></li>
			</ul>
			<div class="filter item all big tall">
				Big and Tall
			</div>
				<div class="filter item all short small">
				Short and Small
			</div>
		 <div class="filter item all big short">
				Big and Short
			</div>
				 <div class="filter item all tall small">
				Tall and Small
			</div>
				 <div class="filter item all big few">
				Big and Few
			</div>
				 <div class="filter item all many small">
				Many and Small
			</div>
				 <div class="filter item all few small">
				Few and Small
			</div>
			 <div class="filter item all short few small">
				Short and Small and Few
			</div>
				 <div class="filter item all big tall many">
				Big and Tall and Many
			</div>

CSS

body{
	font-family: arial;
	font-size: small;
}
.item{
	height: 50px;
	width: 50px;
	padding: 10px;
	margin: 10px;
	background-color: grey;
	text-align: center;
	color: white;
	overflow: wrap;	
  float: left;
}
ul{
  list-style-type: none;
  
  margin-left: -25px;
}
li {
   display: inline;
}
.filter-button{
  width: 50px;
 margin-bottom: 5px;
  
}
.filter-button:hover{
	text-decoration: underline;
	color: blue;
	cursor: pointer;
}

JavaScript

$(document).ready(function () {
            //declare a global variable
            var filterVal;
            //check if sessionStorage exists and if so, if there is a var called fillTerm
            //if not, set it to a default value (all)
            if (sessionStorage && sessionStorage.getItem("filTerm")) {
                filterVal = sessionStorage.getItem("filTerm");
            } else {
                filterVal = "all";
                sessionStorage.setItem("filTerm", filterVal);
            }

            //now let's attach some interaction to our buttons
            $(".filter-button").on("click", function () {
                //get the value for our filter
                filterVal = $(this).attr("data-filter");
                //store it in the session storage
                sessionStorage.setItem("filTerm", filterVal);
                console.log(sessionStorage);
                console.log(filterVal);
                //call our view update function
                updateView();
            });

            //this is the function that manipulates the UI
            function updateView() {
                //default situation: all is visible
                if (!filterVal || filterVal === "all") {
                    $('.filter').show();
                }
                    //hide all and show filtered values
                else {
                    $(".filter").hide();
                    $('.filter').filter('.' + filterVal).show();
              
                    console.log("searchTerm");
                    console.log("filterVal");
                }
            };
            //update the view when the page loads
            updateView();
       
            $(".searchButton").click(function () {
            var term = $(this).attr('data-filter');
            var searchTerm = document.getElementById("searchEntry").value;
       
            console.log("searchTerm");
            console.log("filterTerm");
        })

        });