I have a list of filter buttons that filter gallery items. Filtering works well and the state of the filter is stored in the session store. I am trying to link a search box and a button.
The behavior that I am trying to achieve is when someone enters the search field, then clicks the search button or clicks the button, their record is passed to the filter function and the results of the comparison are displayed.
I find it difficult to get a string and assign it to a variable. I had a problem with the filter working on this variable.
I made a fiddle to demonstrate: https://jsfiddle.net/ewebster/Lxveeq65/22/
JS:
$(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 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"); }) });
The searchButton function at the end is what doesn't work. I understand that a button can call a function when clicked, or a function can listen to a click on the button ID. In any case, I could not pick up the input for the search.
The form may require a POST method, depending on where I ask.
javascript jquery filter forms
Webster
source share