Skip to content

Instantly share code, notes, and snippets.

@jlittlejohn
Created September 10, 2014 16:43
Show Gist options
  • Save jlittlejohn/9a60294afc58f48cea80 to your computer and use it in GitHub Desktop.
Save jlittlejohn/9a60294afc58f48cea80 to your computer and use it in GitHub Desktop.
JS: Create A-Z Filtering of a List
$("#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