views:

506

answers:

2

i have two select box, now what i want is i want to move option from one select box to another via a button

below is my code:

php

<table>
    <tr>
        <td>
        <select  size="5" id="suppliedMovies" name="suppliedMovies">
            <option selected="selected" value="">Select Movie</option>          
            <option value="1">sholay</option>
            <option value="3">Jism</option>
            <option value="4">Rog</option>
            <option value="5">Zeher</option>
            <option value="6">Awarpan</option>
            <option value="7">Paap</option>
            <option value="8">paanch<option>
            <option value="9">no entry</option>
        </select>
        </td>
        <td>
            <input type="button" id="toRight" value="&gt;&gt;"><br>
            <input type="button" id="toLeft" value="&lt;&lt;">
       </td>
       <td>
        <select size="5" id="accquiredMovies" name="accquiredMovies">   
            <option> No item right now</option>
        </select>
       </td>
   </tr>
</table>

Jquery

jQuery(document).ready( function() {

 function displayVals() {
      var myValues = jQuery("#suppliedMovies").val();
      return myValues;
    }

    jQuery('#toRight').click(function(){
    var x=displayVals(); 
    console.log(x);
    var txt=jQuery("#suppliedMovies option[value='"+x+"']").text();
    console.log(txt);
    if(x!=''){
    jQuery('#accquiredMovies').append("<option value='"+x+"' >"+txt+"</option>");
    }           
    });         
});

i m using above jQuery, that is working fine but, i want that

when one item is copy from one select box to another select box, then that item should be disable(or probably delete) from first select box (to prevent duplicate entry). i also want to move item from right to left select box please suggest me optimized jQuery . Thanks.

UPDATE

if i want to use multiple select box on both side? then how do i use that?

more update

if i click on a item on rightselectbox and move it to left selectbox, and go further(post) then on right selectbox, there is nothing selected items?? what i need is on right selectbox, there all items shoud be always selected , otherwise what i need to do? after i move an item from right to left ,i again have to select rest of items on right selectbox and go further

solution for my update part

i used below code, please take a look and suggest me if somebody finds any mistake/error in this. Thanking You

jQuery('form').submit(function() {  
        jQuery('#accquiredMovies option').each(function(i) {  
            jQuery(this).attr("selected", "selected");  
         });  
    }); 
+1  A: 

You could use jQuery remove. Like:

$('#suppliedMovies option[value=' + x ']').remove()

..fredrik

EDIT:

You could optimize the function like this:

jQuery(document).ready( function() {
    # Store the jQuery object return by the selector so that you don't need to
    # make a new selector request next time a user press the #toRight
    var suppliedSelect = jQuery('#suppliedMovies');

    jQuery('#toRight').click(function () {
        suppliedSelect.find(':selected').each(function (index, elem) {
            var selectElem = $(elem);
            if (selectElem.val()) {
                selectElem.val.appendTo('#accquiredMovies');
            }
         });
    });
});
fredrik
@fredrik Thanks!I used that! but i need optimized code, i think this is too length methos??and ome more thing, if i use muliple selectbox then??
diEcho
I updated my answer with some code.
fredrik
Thanks! one more problem? how do i prevent first option from both select box, bcoz they having no value?
diEcho
I updated the code.
fredrik
U r lifesaver! thankssssssss
diEcho
if i click on a item on rightselectbox and move it to left selectbox, and go further(post) then on right selectbox, there is nothing selected items??what i need is on right selectbox, there shoud always selected for all item? otherwise what i have to do? after i move an item from right to left ,i again have to select rest of items on right selectbox and go further
diEcho
A: 

Hi,

the article is in German, but the linked code contains a good example: Move selected items between checkboxes.

Regards,

Martin Abraham

Martin Abraham