jQuery addClass example

Change class name on click in jQuery

by wisegorilla

HTML

<select name="select1" id="select1">
    <option value="" disabled selected>Select your option</option>
    <option value="1" option-id="1">Sitzen </option>
    <option value="2" option-id="2">Schlafen</option>
    <option value="3" option-id="3">Reisen</option>
</select>

<select name="select2" id="select2" disabled>
 <option value="a" disabled selected option-id="a">Select your option</option>
    <option value="1" option-id="1">Comfort Cushion</option>
    <option value="2" option-id="1">Freilagerungssitzkissen</option>
    <option value="3" option-id="1">Rückenkissen</option>
    <option value="4" option-id="1">Sitzkissen zum Druckmanagement</option>
    <option value="5" option-id="1">Wedge Cushion</option>
    <option value="6" option-id="1">Wedge Pillow</option>
    <option value="7" option-id="2">Comfort Pillow</option>
    <option value="8" option-id="2">Dreamy Cushion</option>
    <option value="9" option-id="2">Kniekissen</option>
    <option value="10" option-id="3">Reise Nackenkissen</option>
</select>

<select name="select3" id="select3" disabled>
    <option value="a" disabled selected option-id="a">Select your option</option>

    <option value="2" option-id="2" >Standard</option>
    <option value="3" option-id="2" >Large</option>

    <option value="1" option-id="3" >Small</option>
    <option value="2" option-id="3" >Standard</option>

    <option value="2" option-id="4" >Standard</option>
    <option value="3" option-id="4" >Large</option>  


</select>

JavaScript

var $select1 = $( '#select1' ),
    $select2 = $( '#select2' ),
    $select3 = $( '#select3' ), // I added that line but not sure if its correct

    $options_a = $select2.find( 'option' ),
    $options_b = $select3.find( 'option' ); // I added that line but not sure if its correct

  $select1.on( 'change', function() {
  $select2.prop("disabled", false);

  $select2.html( $options_a.filter(function () {
    return $(this).attr('option-id') === $select1.val() ||
        $(this).attr('option-id') === "a"
  }))

  $select2.val('a')
    $select3.val('a')
} )

// I added the next lines for select3 but not sure if they are correct
$select2.on( 'change', function() {
    $select3.prop("disabled", false);
  $select3.html( $options_b.filter(function () {
    return $(this).attr('option-id') === $select2.val() ||
        $(this).attr('option-id') === "a"
  }));

$select3.val('a')

} )