Filtering html select listbox items

The efficient way of filtering select list box items may be achieved using JQuery

$(function(){
    $('#filterInput').keyup(function () {
        doFilter(this);
    });
});

/* filter method */
function doFilter(item) {
    $(item).val($(item).val().toUpperCase());
    var valFilter = $(item).val();
    var valFilter2 = null;
    if ($(item).val().length > 2)
        valFilter2 = $(item).val();
    $('#listItems>option').each(function () {
        var text = $(this).text();
        (text.indexOf(valFilter) == 0 || (valFilter2 != null && text.includes(valFilter2) == true)) ? $(this).show() : $(this).hide();
    });
}


Comments

Popular posts from this blog

Custom ActionResult for Files in ASP.NET MVC - ExcelResult

Platform independent simple password manager application (Electron & NodeJS)