Created
August 22, 2016 21:02
-
-
Save kieranstartup/93c621f7531e1d5b9ac887317f504f65 to your computer and use it in GitHub Desktop.
Store a class from another page using localstorage and call it again on a specific template page
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| jQuery(document).ready(function() { | |
| if (localStorage) { | |
| // LocalStorage is supported! | |
| var firstClass, secondClass, thirdClass; | |
| //get the class of the div that's just been clicked | |
| jQuery('.col-1-6').click(function() { | |
| localStorage.clear(); | |
| firstClass = jQuery(this).find('.mini-thumbnail-individual-image:first-child').attr("class"); | |
| secondClass = jQuery(this).find('.mini-thumbnail-individual-image:nth-child(2)').attr("class"); | |
| thirdClass = jQuery(this).find('.mini-thumbnail-individual-image:last-child').attr("class"); | |
| localStorage.setItem("firstDivClass", firstClass); | |
| localStorage.setItem("secondDivClass", secondClass); | |
| localStorage.setItem("thirdDivClass", thirdClass); | |
| }); | |
| //convert the string of the last clicked div into a class and then work your magic | |
| var currentFirstClass = localStorage.getItem("firstDivClass"); | |
| var currentSecondClass = localStorage.getItem("secondDivClass"); | |
| var currentThirdClass = localStorage.getItem("thirdDivClass"); | |
| // If body class is single i.e. a single project, then update the class with the one from the thumbnails on the homepage | |
| if (jQuery('body').hasClass('single')) { | |
| console.log("single post"); | |
| jQuery('.individual-project:nth-child(3n+1)').addClass(currentFirstClass).removeClass('mini-thumbnail-individual-image'); | |
| jQuery('.individual-project:nth-child(2n+2)').addClass(currentSecondClass).removeClass('mini-thumbnail-individual-image'); | |
| jQuery('.individual-project:nth-child(3n+3)').addClass(currentThirdClass).removeClass('mini-thumbnail-individual-image'); | |
| } else { | |
| console.log("nope"); | |
| } | |
| } else { | |
| // No support. Use a fallback such as browser cookies or store on the server. | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment