scrit
scrit

Reputation: 241

javascript for listbox items

Please suggest how can i override the item in the list if it is already available in the list when user moves from one listbox to other list box.

Upvotes: 0

Views: 333

Answers (1)

Mohamed-Yousef
Mohamed-Yousef

Reputation: 24001

you can just use jquery insertAfter

    $(document).ready(function(){
    $('.addtoright').on('click',function(e){
        e.preventDefault();
        $('#s option:selected').each(function(){
            if( $('#d option:contains("'+$(this).text()+'")').length > 0){ 
                $(this).remove();
            }else{
                if($('#d option').length > 0){
                   $(this).insertAfter('#d option:last');
                }else{
                    $(this).appendTo('#d');
                }            
            }
        });
    });
    $('.addtoleft').on('click',function(e){
        e.preventDefault();
        $('#d option:selected').each(function(){
            if( $('#s option:contains("'+$(this).text()+'")').length > 0){ 
                $(this).remove();
            }else{
                if($('#s option').length > 0){
               $(this).insertAfter('#s option:last');
                }else{
                    $(this).appendTo('#s');
                }
            }
        });
       });
});

DEMO HERE

Dont forget to add class addtoright to right arrows and addtoleft to left arrows

Upvotes: 1

Related Questions