0

I want to implement following functionality.

There will be 'n' number of 'select' tags and on selecting the value (on change event) that selected value will be removed from other 'select' tag. If the user again changes the value in current selected drop down list, I want to add previous selected value in other 'select' tag at its original position.

I am able to do remove part and partially append part.(need to append previous value at it's original position.)

HTML:
<div class="container">
    <input id="preval" class="hidden" type="text" value="" name=""/>
    <select class="selectoption">
        <option value="1">1</option>
        <option value="ab">ab</option>
        <option value="xysd">xysd</option>
        <option value="4">4</option>                
    </select>
</div>
<div class="container">
    <select class="selectoption">
        <option value="1">1</option>
        <option value="ab">ab</option>
        <option value="xysd">xysd</option>
        <option value="4">4</option>                
    </select>
</div>
<div class="container">
    <select class="selectoption">
        <option value="1">1</option>
        <option value="ab">ab</option>
        <option value="xysd">xysd</option>
        <option value="4">4</option>                
    </select>
</div>

CSS:

.hidden{
    display:none;
}

JAVASCRIPT:
$(document).ready(function(){

    $(".selectoption").change(function(e){
                var currentvalue = $(e.currentTarget).val();    
    /*to remove selected option from other drop down list  */ $('.selectoption').not($(e.currentTarget)).children('option[value='+currentvalue+']').remove();
            var currindex = $(e.currentTarget).index();
            var prevval = $('#preval').val();       
            var previndex = $('#preval').attr("name");

        $('#preval').val(currentvalue);
        $('#preval').attr('name',previndex);
        /*apped previous value */
        if(previndex == currindex){
                    $('.selectoption').not($(e.currentTarget)).append('<option value='+prevval+'>'+prevval+'</option>');                    
                }
    });        
});


JSFiddle:
http://jsfiddle.net/GG3S5/1/
user2243832
  • 59
  • 1
  • 1
  • 7

1 Answers1

1

Please simplify your question next time, do you want to hide the selected <option> in other <select>?

Hope this help, http://jsfiddle.net/GG3S5/9/

Steely Wing
  • 16,239
  • 8
  • 58
  • 54