Skip to content Skip to sidebar Skip to footer

How To Filter A Dropdown List Based On A Already Pre Selected List

I am trying to filter the dropdown list based on the first pre-selected list. The first list is contains 'United States' as pre selection and the list to be filtered is the second

Solution 1:

jQuery

Get the selected country and then find the <option> using attribute-equals-selector and hide the siblings

$(function () {
    var country = $('.selected').data('country');
    $('#CountryCode').find('[value="' + country + '"]').siblings().hide();
    $('#CountryCode').val(country);
});

HTML

Add data-* attribute to the html elements

<ul><lidata-country="ARG">Argentina</li><lidata-country="USA"class="selected">United States</li><lidata-country="AUS">Australia</li></ul>

DEMO

Post a Comment for "How To Filter A Dropdown List Based On A Already Pre Selected List"