compare two select options and hide the option values if it is  greater than the selected value

Yakup K
Yakup K used Ask the Experts™
on
I have got two select elements and I would like hide the option values if it is greater than the first select options values.

<select name="upgradeQuoteDetails[0].currentBearer">
            <option value="10">
            10 Mbps</option>
            <option value="100">
            100 Mbps</option>
            <option value="1000">
            1 Gbps</option>
            <option value="10000">
            10 Gbps</option>
            <option value="0">
            Other</option>
    </select>
    
<select name="upgradeQuoteDetails[0].quotedBandwidths">
            <option value="10">
            10 Mbps</option>
            <option value="20">
            20 Mbps</option>
            <option value="30">
            30 Mbps</option>
            <option value="50">
            50 Mbps</option>
            <option value="100">
            100 Mbps</option>
            <option value="1000">
            1 Gbps</option>
            <option value="10000">
            10 Gbps</option>
            <option value="0">
            Other</option>
    </select>

Open in new window





$("select[name='upgradeQuoteDetails[0].currentBearer']").change(function(){
        	var selectedBearer =$(this).children("option:selected").val();
          
          
          var options = 		            $("select[name='upgradeQuoteDetails[0].quotedBandwidths'] option");

    var values = $.map(options ,function(option) {
        return option.value;
    });
    
    for (var i=0; i<values.length; i++){
    console.log(values[i]);
  
    		if (values[i] >= selectedBearer) {
        	        
                      $("select[name='upgradeQuoteDetails[0].quotedBandwidths']").children('option[value='+values[i]+']').hide();

        }
    }

       
});
   

Open in new window


The bandwidth option value should always be less then the bearer options values.  For example, if the bearer option 100, the bandwidth should show 10, 20, 30, 50 and 100 only.jsfiddle
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
Hey Yakup,

Take a look at this:

$("select[name='upgradeQuoteDetails[0].currentBearer']").change(function() {
    var selectedValue = parseInt($(this).val());

    $("option", "select[name='upgradeQuoteDetails[0].quotedBandwidths']").each(function(i, e) {
        $(e).css( 'display' , (parseInt(e.value) > selectedValue) ? 'none' : 'block' );
    })
});

Open in new window

Here's a working fiddle

Author

Commented:
Excellent.! Thanks a lot Chris.

Author

Commented:
Hi Chris,

Thanks once again for your support.

That solution works on Chrome, however it does not work on the internet explorer. Any recommendation ?

Thank you.
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
Hmm. A constant reminder why Internet Explorer just needs to die ... completely.

First off, ask yourself ... do you REALLY need to support IE. It has such a tiny percentage of the market these days, and the general thinking is that the longer developers keep supporting it, the longer it will keep going .. and trust me ... that's not a good thing!

By supporting IE in your applications, you will potentially be adding 20% (un-scientific guess) to the time and cost that it takes you to develop sites. Always ask the client if they're sure they want IE support. If they say yes, tell them it'll cost another 20% (surprising how many suddenly decide that they don't actually need support after all).

Right. Rant Over :)

IE does not support display:none on <option> tags, so you have a couple of choices. The easiest is to just disable the option instead of hiding it. In the code above, change line 5 to this:

$(e).prop('disabled', (parseInt(e.value) > selectedValue) ? true : false );

Open in new window

The options will still be visible, but the user can't select them.

The other option is to remove them from the DOM entirely. This is more complicated because you'd need to store all the <options> into a variable so that you can re-add them back in when the first dropdown changes.

And of course - you can't test this in JSFiddle, because ... JSFiddle doesn't support Internet Explorer :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial