0

I have five select boxes .all have an first option "--Select--" and i dont i want to have select boxes have the same option selected ,no duplicate options .how to do this using jquery

3
  • The question is not clear, show the appropriate HTML code. Commented Aug 14, 2011 at 9:29
  • its about how to avoid duplicate values across the multiple select boxes using jquery Commented Aug 14, 2011 at 9:34
  • You use "text boxes" then "select boxes" VERY loosely. show some code. Commented Aug 14, 2011 at 9:51

1 Answer 1

1

First give all the selects the same class and different id's. Then something like this.

var selects = $('.days');
$('.days').change(function(){
   var value = $(this).val();
   var count = 0;
   for(var i=0; i<selects.length; i++){
      if($(this).attr('id') != $(selects[i]).attr('id')) {
         var checkVal = $(selects[i]).val();
         if(value == checkVal) {
            alert("Value already selected, please select a different value");
            $("#"+$(this).attr('id')+" option[value='']").attr("selected", "selected");
         }
      }
   }

});

I've added a JSFiddle showing the solution.

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

3 Comments

the last line? If you mean $("#"+$(this).attr('id')+" option[value='']").attr("selected", "selected"); Basically it takes the select element that has been changed and the option with the given value (blank in this case) is set as selected.
Suppose if i want to check for duplicate values across the multiple input text boxes, what is the logic
Do you mean you want to compare the value in an input or textarea with the values in other inputs or textareas to see if the value is the same in more than one? Start a JS Fiddle with your html.

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.