Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter Category Select Drop down based on Another drop down option

Tags:

jquery

I want to filter the list of options based on the selection of another drop down.

Please see the jquery code below; i am sure there is a tiny bit that i am missing that's why it is not working.

if($('#selectionone').is(':selected')){
    $('option').filter('.edibles');   

}
if($('selectiontwo').is(':selected')){
  $('option').filter('.vegfats');
}

Here is the jsfiddle link

like image 600
Joe Ng'ethe Avatar asked Dec 09 '13 17:12

Joe Ng'ethe


People also ask

How do I filter the second dropdown by selection of the first dropdown?

Go to the Conditionals tab of the second dropdown. Create a new condition that when 1st Dropdown's value is Mobile, the Choices will be as you define. And that's it. The Choices of the second dropdown will vary depending upon the option selected in the first dropdown.

How do I change a selection based on another dropdown?

We want to change the selects options based on other dropdowns so we will follow the below steps. Create a state variable that is initially null or undefined. Create different arrays for different dropdowns. Create a local variable say type that will hold any of the array based on the dropdown selected.

How do I make one filter dependent on another in Excel?

Select the cell where you want the Dependent/Conditional Drop Down list (E3 in this example). Go to Data –> Data Validation. In the Data Validation dialog box, within the setting tab, make sure List in selected. In the Source field, enter the formula =INDIRECT(D3).

How do I filter Data from a drop down list in Excel?

Creating the Drop Down FilterGo to Data –> Data Validation. In Data Validation dialogue box, select the Settings tab. In Settings tab, select “List” in the drop down, and in 'Source' field, select the unique list of countries that we generated. Click OK.


2 Answers

Here is my approach of adding/removing options based on the options selected and this will work in most of browsers.

I have modified the html by adding class to first select options like

<option class="edibles" value="Edible Oils" id="selectionone">Edible Oils</option>
<option class="vegfats" value="Vegetable Cooking Fats" id="selectiontwo">Vegetable Cooking Fats</option>

JS:

$(document).ready(function () {    
    var allOptions = $('#selectprod option')
    $('#selectcat').change(function () {
        $('#selectprod option').remove(); //remove all options
        var classN = $('#selectcat option:selected').prop('class'); //get the 
        var opts = allOptions.filter('.' + classN); //selected option's classname
        $.each(opts, function (i, j) {
            $(j).appendTo('#selectprod'); //append those options back
        });
    });
});

JSFiddle

like image 194
Praveen Avatar answered Oct 19 '22 03:10

Praveen


This is a simple approach to the issue, which should be rather solid:

<select class="masterSelect">
    <option value="0">Fiat</option>
    <option value="1">Opel</option>
    <option value="2">Porsche</option>
</select>

<div class="hiddenOptions">
    <select>
        <option value="1">Punto</option>
        <option value="2">Mani</option>
        <option value="3">Pierri 203</option>
    </select>

    <select>
        <option value="4">Elysis</option>
        <option value="5">201</option>
        <option value="6">Mido</option>
    </select>

    <select>
        <option value="7">911</option>
        <option value="8">Macho</option>
        <option value="9">Vanguard</option>
    </select>
</div>

css:

.hiddenOptions > select
{
   display: none;
}

.hiddenOptions > select.active
{
   display: inline-block;
}

JS:

$('.masterSelect').on('change', function() {
    $('.hiddenOptions select').removeClass("active");
    $('.hiddenOptions select').eq($(this).val()).addClass("active");

    showValue();
});

$('.hiddenOptions select').on('change', function() {
    showValue();
});

$('.masterSelect').trigger('change'); //set initial value on load

function showValue()
{
    console.log($('.hiddenOptions select.active').val());
}

https://jsfiddle.net/g5h2k6t4/1/

like image 22
Håkon Egset Harnes Avatar answered Oct 19 '22 04:10

Håkon Egset Harnes