0

I have the following two select boxes

<select id="first" onchange="hideShowOptions(this.value)">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
</select>

and

<select id="second" >
    <option value="5">5</option>
    <option value="6">6</option>
    <option value="7">7</option>
    <option value="8">8</option>
</select>

and follwoing is my script

function hideShowOptions(selectedValue) {
    if(selectedValue == 1 || selectedValue == 2 || selectedValue == 3) {
        $('#second').children('option[value="8"]').css('display','none');
    } else {
        $('#second').children('option[value="8"]').css('display','inline');
    }
}

It correctly sets style="display: none" as per the above code but the option is not hidden from the UI and I am still able to see the option.

The goal is to hide certain option from second select based on the value selected from first select.

halfer
  • 19,471
  • 17
  • 87
  • 173
Joe
  • 4,270
  • 17
  • 56
  • 101

2 Answers2

0

The following is the answer, courtesy to gugateider

function hideShowOptions(selectedValue){
    if(selectedValue == 1 || selectedValue == 2 || selectedValue == 3)
    {
        $('#second').children('option[value="8"]').attr('disabled', ''); // if doesn't support will just disable
      //   $('#second').children('option[value="8"]').hide(); // if supports will hide
    }else{
        $('#second').children('option[value="8"]').removeAttr('disabled'); // if doesn't support will just enable
        //$('#second').children('option[value="8"]').show(); // if it supports, it will display
    }

}
Joe
  • 4,270
  • 17
  • 56
  • 101
0

<select id="first" onchange="hideShowOptions(this.value)">
                              <option value="1">1</option>
                              <option value="2">2</option>
                              <option value="3">3</option>
                              <option value="4">4</option>
 </select>

function hideShowOptions(x) {

var hideTrigger = [1, 2 ,3];

switch (x){
case hideTrigger[0].toString():
$('#second').children('option[value="8"]').css('display','none');
break;
case hideTrigger[1].toString():
$('#second').children('option[value="8"]').css('display','none');
break;
case  hideTrigger[2].toString():
$('#second').children('option[value="8"]').css('display','none');
break;
default:
$('#second').children('option[value="8"]').css('display','block');
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="first" onchange="hideShowOptions(this.value)">
                              <option value="1">1</option>
                              <option value="2">2</option>
                              <option value="3">3</option>
                              <option value="4">4</option>
                            </select>
                            
                            <select id="second" >
                              <option value="5">5</option>
                              <option value="6">6</option>
                              <option value="7">7</option>
                              <option value="8">8</option>
                            </select>
anon
  • 1
  • You should explain you answer. Please have a look to our Code of conduct: https://stackoverflow.com/conduct – Alberto Sep 17 '18 at 11:01