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,824
  • 17
  • 99
  • 186
Joe
  • 4,460
  • 19
  • 60
  • 106

3 Answers3

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,460
  • 19
  • 60
  • 106
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
0

Using only vanilla js:

const second = document.getElementById( "second")
const eight  = document.getElementById( "eight" )

function hideShowOptions( selectedValue ){
    if ( 
         selectedValue == 1 || 
         selectedValue == 2 || 
         selectedValue == 3
       ){
         eight.style.display = "none"
         second.value = 5 
           // prevents bug if user reselects 1,2 or 3
           // after selecting 8
        } else {
         eight.style.display = "inline"
        }
}
#eight {
  display: none
}
<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" id="eight">8</option>
</select>
Andrei Cleland
  • 378
  • 3
  • 10