Created
September 10, 2014 16:43
-
-
Save jlittlejohn/9a60294afc58f48cea80 to your computer and use it in GitHub Desktop.
JS: Create A-Z Filtering of a List
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
$("#glossary > .term").each(function() { | |
$(this).addClass("glossary-" + $(this).children("h3").text().charAt(0)); | |
}); | |
$('#glossary > #no-results').hide(); | |
var resetDirectory = function () { | |
$("#glossary > .term").show(); | |
}; | |
(function filterDirectory () { | |
function setActiveChar(el){ | |
$("#directory > a").removeClass("active"); | |
$(el).addClass("active"); | |
} | |
$("#directory > a").on("click", function() { | |
if ($(this).hasClass("active")){ return false; } | |
$('#glossary > #no-results').hide(); | |
var href = $(this).attr("href").toString(); | |
if (href === "#all") { | |
setActiveChar(this); | |
resetDirectory(); | |
} | |
else { | |
setActiveChar(this); | |
var character = href.slice(1); | |
var terms = $("#glossary > .term[class$='" + character + "']").show(); | |
$("#glossary > .term:not([class$='" + character + "'])").hide(); | |
if(!terms.length){ $('#glossary > #no-results').show(); } | |
} | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment