1

I want to change the second dropdown based on the selection of the first one, but it is not working right now. I'm using bootstrap select's selectpicker and form-group class, although I'm not sure if it caused the problem.

The second dropdown is disabled before the first one is selected, and only show items that match the data group selection.

HTML:

<select id="seasons" class="selectpicker form-group" title="Choose season" onchange="filterSeason();">
    <option value='2013-14'>2013-14</option>
    <option value='2014-15'>2014-15</option>
    <option value='2015-16'>2015-16</option>
    <option value='2016-17'>2016-17</option>
</select>
<select id="clubs" class="selectpicker form-group" title="Choose club" disabled>
    <optgroup data-group='2013-14' label="England">
        <option data-group='2013-14' value='ENG1'>Team 1</option>
        <option data-group='2013-14' value='ENG2'>Team 2</option>
    </optgroup>
    <optgroup data-group='2014-15' label="Spain">
        <option data-group='2014-15' value='SPA1'>Team 3</option>
    </optgroup>
    <optgroup data-group='2015-15' label="Germany">
        <option data-group='2015-16' value='GER1'>Team 4</option>
        <option data-group='2015-16' value='GER2'>Team 5</option>
        <option data-group='2015-16' value='GER3'>Team 6</option>
    </optgroup>
</select>

jQuery:

function filterSeason() {
      var seasons = $("#seasons").find('option:selected').text(); 
      $("#option-container").children().appendTo("#clubs");
      var selectSeason = $("#clubs").children("[data-group!='"+seasons+"']"); 
      selectSeason.appendTo("#option-container");
      $("#clubs").removeAttr("disabled");
};
Victor
  • 21
  • 6

1 Answers1

0

I think this is what you needed if I understood correctly.

use $("#clubs").children().removeAttr('disabled'); to enable all children and then disable which are not relevant with this $(selectSeason).attr('disabled','disabled');

Please check this fiddle

FIDDLE

Muhammed Shevil KP
  • 1,404
  • 1
  • 16
  • 21