Carlos Jhon manlulu Carlos Jhon manlulu - 4 months ago 13
Ajax Question

Show the content of Multiple select

i have this code but does not work i just want to put the content of the multiple combobox into textbox name valueholder and every value should be separated by comma. Thanks in advance, i dont know where to start im new in javascript thx

Example output in the textbox should be.

4800014145089,4800131590977

var ValuesArray = $('#countries').val();
var holder = $('#valueholder').val();
ValuesArray== valueholder;
<input type="text" name="valueholder" id="valueholder">
<select id="countries" class="multiselect" multiple="multiple" name="countries[]">

<option value="4800014145089">4800014145089</option>
<option value="4800131590977">4800131590977</option>

</select>
<br/>

Answer

It's easy with jquery:

$(function(){
    // get all the option tags
    var options = $('#countries option'); 

    // map the function for each option 
    var values  = $(options).map(function() { 
        return $(this).val(); 
    }).get();

    $('#valueholder').val(values.join(',')); 
});

See the working example

Comments