0

I have n number of dropdownlists with the same options and values.

I want to validate it, that only the unique value could be selected among these n dropdownlists. n is random number.

Example:

I have 3 ddl with these values:

<select>
<option value="apple" selected="yes"> Apple </option>
<option value="carrots"> Carrots </option>
<option value="potatoes"> Potatoes </option>
</select>

and I want to have unique selected values for each dropdownlist, so if you select apples and try to select apples again on another ddl, it changes a previous one to the available value or prompts an alert..

4
  • Here's a jsFiddle for you to do the homework on: jsfiddle.net/WQkkV/6 Commented Jun 28, 2011 at 19:43
  • thanks for the link, will do it in firebug instead.. Commented Jun 28, 2011 at 19:47
  • Maybe better prohibit multiple selection of the same option? Right after option has selected make options with the same value in rest dropdowns disabled. Commented Jun 28, 2011 at 20:02
  • that would work also, how do I disable an option in ddl? you mean disable or remove it? Commented Jun 28, 2011 at 20:07

1 Answer 1

1

try this:

 $( function(){
    $('select').change( function(){
        current = $(this).val();
        unique = 0;
        $('select option:selected').each( function(){
            if ($(this).val() == current) unique++;
        });
        if (unique!=1) alert('not unique') ;
    });
});

HTML:

<select>
    <option selected="selected" value="one">one</option>
    <option value="two">two</option>
</select>


<select>
    <option value="one">one</option>
    <option selected="selected" value="two">two</option>
</select>
Sign up to request clarification or add additional context in comments.

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.