Search code examples
jquerymultiple-select

Mutiple select, tried val(), can't get values


So I've read the other question about this and I can't seem to get it to work. I have this HTML:

    <select name="filter" id="filter" multiple="multiple" data-native-menu="false">
    <option>Choose Filter</option>
    <option value="Matt"> Matt</option>
    <option value="Brian"> Brian </option>
    </select>

Yet using this code:

    $("#filter").val()

Returns me an empty string, even when options are selected.


Solution

  • $('#filter').change(function(){
        var selectedValues = $(this).find('option:selected').map(function(){
                                 return this.value;
                             }).get();
        console.log(selectedValues);
    });
    

    JS Fiddle demo.

    References: