2

I need to implement 3 select boxes on a page wherein the value selected in one box should not appear on the next select boxes. Can I have detailed explaination to this with code? A newbie in JS. Let me know if this can be done with PHP too. Thanks in advance. Here goes the sample code.. Let me know why its not taking the JS script:

    <html>
    <head>
<title>check</title>
<body>


<select name="name[]" class="select">
    <option value="">Select</option>
    <option value="volvo">Volvo</option>
    <option value="saab">Saab</option>
    <option value="mercedes">Mercedes</option>
    <option value="audi">Audi</option>
</select>
<select name="name[]" class="select">
    <option value="">Select</option>
    <option value="volvo">Volvo</option>
    <option value="saab">Saab</option>
    <option value="mercedes">Mercedes</option>
    <option value="audi">Audi</option>
</select>
<select name="name[]" class="select">
    <option value="">Select</option>
    <option value="volvo">Volvo</option>
    <option value="saab">Saab</option>
    <option value="mercedes">Mercedes</option>
    <option value="audi">Audi</option>
</select>

<script>
$('select').change(function() {
    var myOpt = [];
    $("select").each(function () {
        myOpt.push($(this).val());
    });
    $("select").each(function () {
        $(this).find("option").prop('hidden', false);
        var sel = $(this);
        $.each(myOpt, function(key, value) {
            if((value != "") && (value != sel.val())) {
                sel.find("option").filter('[value="' + value +'"]').prop('hidden', true);
            }
        });
    });
});
</script>

</body>
</head>
</html>
4
  • u mean after select first the value of next select box appears based on first selection ? Commented Mar 25, 2016 at 6:23
  • Yeah.. The next select box should not show value selected in first select box and so on. Commented Mar 25, 2016 at 6:27
  • first of all give ids to ur select box that make some difference Commented Mar 25, 2016 at 6:28
  • I tried giving id's. . Still no success @Maninderpreet. Do i need to give ID's to the option field? or the <select> tag Commented Mar 25, 2016 at 6:34

2 Answers 2

6

Hope this answer will solve your query:

$('select').change(function() {
  var val = $(this).val();

  $("select").not(this).each(function(index, item) {
      $(item).find("option[value='" + val + "']").remove();
  });
});

Or you can go through to jsfiddle link for live demo.

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

6 Comments

I tried this from the same URL which you have provided. But I want a step by step guidance. Thanks Do let me know where am I going wrong in the code i provided.
Do I need to download jquery lib first??
yes u need to include jQuery lib first @AnandShankar
Thanks @maninderpreet. Its working.. :) Cheers to helping the world !! :) I might come back for few more solutions on other tpoics related to the question. :)
|
0

Why dont you just use multiple select box?

<select multiple name="name[]" class="select">
    <option value="">Select</option>
    <option value="volvo">Volvo</option>
    <option value="saab">Saab</option>
    <option value="mercedes">Mercedes</option>
    <option value="audi">Audi</option>
</select>

2 Comments

I can. but i need to put three select boxes with selected value not showing up in next and then next.
hi.. I need to add three select boxes with same data coming from database. how to do this with while loop? Any help with code?

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.