0

I am trying to achieve the following thing in my code but it is getting complicated. I have 'n' dropdowns with or without duplicate values in it.

for simplicity lets assume following scenario:

dropdown1:

<select>
<option>100</option>
<option>200</option>
<option>102</option>
</select>

dropdown 2:

<select>
<option>100</option>
<option>200</option>
<option>201</option>
</select>

dropdown3 :

<select>
<option>100</option>
<option>300</option>
<option>301</option>
</select>

case1: if user select value 100 from dropdown 1 then 100 should be removed from all the dropdowns.and when user change dropdown 1 value from 100 to 200 then 100 should be added back to all the dropdowns and 200 should be removed from all the dropdowns.

removing seems easy but adding back values is little difficult.

how can I maintain a list or some other data structure to remember which value to add and where incase of multiple value change? is there any advance jquery feature or generic javacript logic i can use ?

8
  • How about if they select 100 from 1st select and 200 from 2nd select, do you remove 200 from first then? Commented May 18, 2017 at 18:15
  • Will changing the drop down manually happen only for the first drop down ? Commented May 18, 2017 at 18:17
  • yes 200 should get removed from dropdown1 option list Commented May 18, 2017 at 18:18
  • Would it be sufficient to disable the option instead of removing? Commented May 18, 2017 at 18:18
  • @IstiaqueAhmed no user can change any dropdown. Commented May 18, 2017 at 18:20

3 Answers 3

1

If it is sufficient to just disable the option instead of actually removing it, the following could work for you. You might want to adapt the handling of the selects when initially loading the site.

$('select option[value="' + $('select').eq(0).val() + '"]').not(':eq(0)').prop('disabled', true);

$('select').on('change', function() {

  var val = $(this).val();

  $('select option').prop('disabled', false);
  $('select option[value="' + val + '"]').not($(this)).prop('disabled', true);

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select>
  <option value='100'>100</option>
  <option value='200'>200</option>
  <option value='102'>102</option>
</select>
<select>
  <option value='100'>100</option>
  <option value='200'>200</option>
  <option value='201'>201</option>
</select>
<select>
  <option value='100'>100</option>
  <option value='300'>300</option>
  <option value='301'>301</option>
</select>

Sign up to request clarification or add additional context in comments.

1 Comment

+1 Yes I have disabled the selected options in other dropdowns and it works like charm. simple and elegant.
0

It would be better to set display to none instead. Hence, you will avoid the complications of adding or removing in the appropriate order. So, you can easily return them visible.

$( "option" ).each(function( index ) {
    $(this).css("display", "");
});

Comments

0
$("#drop").change(function () {
var selected_value=$(this).val();
var dropdown=$(select);
for(i=0;i<dropdown.length;i++){
$("dropdown[i]   option[value=selected_value]").remove();
}
});

Set id of first dropdown="drop" Here select the value and define it S a variable loop through dropdown with in page remove option when value=selected_value

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.