1

I have a select box that I am getting multiple values from and storing them in an Arrayusing JQuery.

I would like to know how I can then use the contents of this Array to populate options in another select box.

So currently I've got this:

 $('#addBtn').click(function(e){
        e.preventDefault();
        //Get the selected Items in the dropdown 1
        var selected = new Array();
        $('#compresult option:selected').each(function(){
            selected.push($(this).val() + " " + $(this).text());//Add to Array
        });

        //Update contents of dropdown 2  with the Array 
     });

My HTML for dropdown is :

<select multiple="multiple" name="contributors[]" id="compresult">
    <option value="0"></option>
    <option value="3610">X</option>
    <option value="3605">Y</option>
    <option value="335">Z</option>
 </select>

If I select option X and Y my selected array in my JS Code outputs this:

Array [ "3610 X", "3605 Y" ]

How can I then use these values to populate another dropdown? i'm trying to implement an ADD/REMOVE from list functionality type thing.

EDIT: EXPECTED OUTPUT OF DROPDOWN 2

<select multiple="multiple" name="dropdown2" id="compresult">
        <option value="3610">X</option>
        <option value="3605">Y</option>
</select>
3
  • Please add expected output (eg what the html of dropdown2 should look like) Commented Jul 28, 2014 at 10:49
  • @user574632 I've done that now Commented Jul 28, 2014 at 10:50
  • 1
    The id's must be unique, change the name of second select. I think it's a TYPO. Commented Jul 28, 2014 at 10:51

3 Answers 3

3

ID should be unique id="compresult">

 $('#addBtn').click(function (e) {
    e.preventDefault();

    var selected = $('#compresult option:selected').clone(); // get the selected option and copy that element 
    $("#compresult1").html(selected); // insert into new dropdown list 

});

UPDATED DEMO

Sign up to request clarification or add additional context in comments.

1 Comment

@Javacadabra .Do not think about anything , i think it is best solution compare to other one ,It may be very useful for searching users
2

Just build the html string in your loop:

$('#addBtn').click(function(e){
    e.preventDefault();
    //Get the selected Items in the dropdown 1
    var drop2html = '';
    $('#compresult option:selected').each(function(){
        drop2html += '<option value="' + $(this).val() + '">' + $(this).text() + '</option>';
    });

    $('#drop2').html(drop2html);
});

Comments

0

Change the push line to

selected.push([$(this).val(), $(this).text()]);

...so we have the option value and text held separately, not concatenated as one string.

Then, to create the new select (you don't say if you want to create a new one or populate an existing one - I'll assume the latter):

var other_sel = $('#other_select').empty();
$.each(selected, function(i, arr) {
    $('<option /', {value: arr[0], text: arr[1]}).appendTo(other_sel);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.