Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Moving Items from one list to another based on the combobox selection

Tags:

jquery

I have two combobox one with all available Countries name and other is empty like:

Countries

<select name="countryId[]" multiple="multiple" id="countryId" size="10">
    <option id="" value="1" >Afghanistan</option>
    <option id="" value="3" >Albania</option>
    <option id="" value="4" >Algeria</option>
    <option id="" value="5" >American samoa</option>
</select>

Empty

<select name="mycountryId[]" multiple="multiple" id="mycountryId" size="10">
</select>

Now if I select any country from Countries list it should move to Empty list but with selected attribute and exact value some thing if I select three countries from Countries list result should be:

Countries

<select name="countryId[]" multiple="multiple" id="countryId" size="10">
    <option id="" value="1" >Afghanistan</option>
</select>

Empty

<select name="mycountryId[]" multiple="multiple" id="mycountryId" size="10">
    <option selected="selected" id="" value="3" >Albania</option>
    <option selected="selected" id="" value="4" >Algeria</option>
    <option selected="selected" id="" value="5" >American samoa</option>
</select>

and similarly if I clicked on any country from my new country list (named Empty) then the selected country should move back to available countries list with un-selected attribute and exact value like Countries Afghanistan Algeria

I follow Jquery - Moving Items from one list to another based on the combobox selection it is very nice piece of code but how to make all values selected which are moving in targetCars? with selected attribute and vise versa?

like image 310
PHP Ferrari Avatar asked Dec 13 '22 00:12

PHP Ferrari


2 Answers

enter image description here

function MoveListBoxItem(leftListBoxID, rightListBoxID, isMoveAll) {
    if (isMoveAll == true) {

        $('#' + leftListBoxID + ' option').remove().appendTo('#' + rightListBoxID).removeAttr('selected');

    }
    else {

        $('#' + leftListBoxID + ' option:selected').remove().appendTo('#' + rightListBoxID).removeAttr('selected');
    }
}
like image 174
Rikin Patel Avatar answered May 25 '23 06:05

Rikin Patel


First, why your are using [] in your control ids? you can move Items from one list to another using this,

      $('#countryId option:selected').appendTo('#countryId');

for more on operation on select elements Manipulating Select options

like image 22
Furqan Hameedi Avatar answered May 25 '23 06:05

Furqan Hameedi