Martin
Martin

Reputation: 1253

JS make a list of multiple select-options in textField

 $().ready(function() {  
       $('#add').click(function() {  
        return !$('#select1 option:selected').remove().appendTo('#select2');  
       });  
       $('#remove').click(function() {  
        return !$('#select2 option:selected').remove().appendTo('#select1');  
       });  
      });  

These two functions move selected options from one multiple select to another. I need to make a list with all added options' values(in #select2) in a input type hidden. And this input should be updated each time one of these functions is called.

Upvotes: 0

Views: 300

Answers (1)

topp
topp

Reputation: 150

This should help

var yourInput = $('#IdOfYourInput');

$('#add').click(function() {  
    var opt = !$('#select1 option:selected').remove().appendTo('#select2');
    yourInput.val($('#select2 option').map(function(){return this.value;}).get().join(','));
    return opt;
});

Removing is analogous:

$('#remove').click(function() {
    var opt =$('#select2 option:selected').remove().appendTo('#select1');
    yourInput.val($('#select2 option').map(function(){return this.value;}).get().join(','));
    opt.appendTo('#select1');  
    return opt;
});

I hope this is what your were aiming for..

Upvotes: 2

Related Questions