0

I have 3 dropdown lists of same elements. Values selected in first list should not get displayed in second dropdown list. Values selected in second list should not get displayed in third dropdown list. How to do it using jquery?

3
  • $(".ACL").change(function(event){ //alert($("#ACL1 option:selected").val()); preferenceObject["acl1"]=$("#ACL1 option:selected").val(); //alert("acl1 pushed"); //alert($("#ACL2 option:selected").val()); preferenceObject["acl2"]=$("#ACL2 option:selected").val(); //alert("acl2 pushed"); preferenceObject["acl3"]=$("#ACL3 option:selected").val(); }); Commented Sep 17, 2012 at 8:19
  • <div class="ACL"> ACL : <select name="ACL1" id="ACL1"> <% for(int i=0; i< ACLList.size(); i++) { if(ACLList.get(i)!=null){%> <option value="<%= ACLList.get(i) %>"><%= ACLList.get(i) %></option> <%} } %> </select> Commented Sep 17, 2012 at 8:21
  • similarly i created acl2 acl3 in div acl. bt nw i dunno wt to do!! m fetchin acllist by request.getattribute Commented Sep 17, 2012 at 8:23

3 Answers 3

2

Check out demo: http://jsfiddle.net/ezKDB/

Jquery Plugin: https://github.com/WilliamHuey/mutually_exclusive_list_jquery

Usage:

$('select.classNameForEachList').onlyBeOne();
Sign up to request clarification or add additional context in comments.

Comments

0

Here is an example:

HTML

<select class="sel-1">
    <option value="1">One</option>
    <option value="2">Two</option>
    <option value="3">Three</option>
</select>


<select class="sel-2">
    <option value="1">One</option>
    <option value="2">Two</option>
    <option value="3">Three</option>
</select>


<select class="sel-3">
    <option value="1">One</option>
    <option value="2">Two</option>
    <option value="3">Three</option>
</select>

jQuery

var allSelects = $('select[class^=sel-]');
allSelects.on('change', function() {
  allSelects.find('options').show();
  allSelects.not(this).find('option[value="'+ this.value +'"]').hide();
});

The working demo

Hope this will give you an idea.

6 Comments

what is this?--> $('select[class^=sel-]');
dis is wt i wanted... thank u so much :-) . ll try to use it in my code. Bt m using id for each dropdown list
@anil ok, you can use.. but using a common class is easier... its your choice. but don't forget to accept as answer
$('select[class^=sel-]'); m nt gettin dis line.
@anil it selected all select box whose class name start with sel-.
|
0

Here is possible solution:

<html>
<body>
<select id="first" data-child="#second">
<option value="1">one</option>
<option value="2">two</option>
<option value="3">three</option>
<option value="4">four</option>
<option value="5">five</option>
<option value="6">six</option>
<option value="7">seven</option>
</select>

<select id="second" data-child="#third">
</select>

<select id="third">
</select>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
<script type="text/javascript">

$(document).ready(init);

function mapItems(index, elem){
    var items = [];
    items.push({value:$(elem).val(), text:$(elem).text()});    
    return items;
}

function replaceItems(items, appendTo){
    appendTo.empty();
    for(var i in items){
        var item = items[i];
        if(item.value && item.text){
            appendTo.append("<option value='"+item.value+"'>"+item.text+"</option");
        }
    }
}

function selected(e){        
    var items = $(this).find("option:not(:selected)").map(mapItems);
    var child = $($(this).attr("data-child"));
    replaceItems(items, child);
    child.change();
}

function init(){
    var first = $("select#first");    
    var second = $("select#second");    
    var third = $("select#third");
    first.change(selected);
    second.change(selected);
    first.trigger('change');
}​
</script>
</body>
</html>

It is not very pretty, but it works. See demo here.

UPDATE I have cleaned code up. This is link to last version: http://jsfiddle.net/goranobradovic/JLMJn/2/

UPDATE 2 I have put all html here, just copy it to empty html file and it works.

4 Comments

when i pasted d same code in eclipse.. it s showin nthn in 2nd n 3rd list
Paste to notepad first, jsfiddle sometimes puts some weird character at the end
dat is fine.. bt it is nt showin any value in 2nd and 3rd list. html code i wrote in body and js code in head
If you took this js code, you need to use data-child attributes in html, like in jsfiddle. First select has data-child="#idofsecondselect" and second select has data-child="#idofthirdselect"

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.