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

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
Yakup KAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris StanyonWebDevCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Yakup KAuthor Commented:
Excellent.! Thanks a lot Chris.
Yakup KAuthor 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.
Chris StanyonWebDevCommented:
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 :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
HTML

From novice to tech pro — start learning today.