Skip to content

Instantly share code, notes, and snippets.

@joram77
Created October 17, 2023 16:41
Show Gist options
  • Save joram77/fc9c6a26f5dfc574ce23cda3b6c40845 to your computer and use it in GitHub Desktop.
Save joram77/fc9c6a26f5dfc574ce23cda3b6c40845 to your computer and use it in GitHub Desktop.
// given input#search, .my-menu a, .my-menu .sub-menu .a
jQuery(function(){
jQuery('#search').on('keyup',function(){
if(typeof(search_sidebar_timer)!='undefined'){
window.clearTimeout(search_sidebar_timer);
}
search_sidebar_timer = window.setTimeout(function(){
jQuery(".my-menu a").hide();
var txt_show_filter = jQuery('#search').val().toLowerCase().trim();
if(txt_show_filter == '') {
jQuery(".my-menu a").show(); //reset: show all elements
return;
}
jQuery(".my-menu > a").each(function () {
var txt = jQuery(this).text().toLowerCase();
console.log(txt);
if (txt.indexOf(txt_show_filter) > -1 ) {
jQuery(this).show();// show all exact matching elements
return;
}
});
jQuery(".my-menu > a").each(function() { // show Main > Submenu A, B, C... links (searched for 'Main')
var txt = jQuery(this).text().toLowerCase();
console.log(txt);
if (txt.indexOf(txt_show_filter) > -1 ) {
jQuery(this).next('.sub-menu').children('a').show(); // show elements of submenu following matched main menu item
}
});
},300);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment