I have a dropdown using Smartmenus Bootstrap addon. And inside the drop-down menu there is a search button. When you type it, it will automatically show that the menu is being filtered. How to make the filtered menu appear only when you click the search button? Please help and thanks in advanced condition.
This is the jsfiddle I made.
Below is the Javascript for the search function.
$("#searchText").on("keyup", function () {
var g = $(this).val().toLowerCase();
var parMenuArr = [];
var count = 0;
$(".menu").each(function () {
var s = $(this).text().toLowerCase();
if (s.indexOf(g) !== -1) {
$(this).closest('.menu').show();
var parMenu = $(this).attr('pm');
if (parMenu != null && parMenu != '') {
var add = 'true';
for (var i = 0; i < parMenuArr.length; i++) {
if (parMenuArr[i] == parMenu) {
add = 'false';
break;
}
}
if (add == 'true') {
parMenuArr[count] = parMenu;
count = count + 1;
}
}
} else {
$(this).closest('.menu').hide();
}
});
$(".menu").each(function () {
var menu = $(this).attr('sm');
if (menu != null && menu != '') {
for (var i = 0; i < parMenuArr.length; i++) {
if (parMenuArr[i].indexOf(menu) != -1) {
$(this).closest('.menu').show();
}
if (menu == parMenuArr[i]) {
$(this).closest('.menu').show();
break;
}
}
}
});
});
source
share