How to return the selected items in a multiple select with jQuery [closed]

-8

I need to get the values of a $ ('select [multiple]'). val () separated by a comma. For example.

HTML code:

<select id="estado" multiple>
<option selected>SP</option>
<option selected>RJ</option>
<option selected>ES</option>
<option>MG</option>
</select>

JavaScript code with jQuery:

const estados = $('#estado').val(); //Resultado: SPRJES    

How to make states return to be "SP, RJ, ES" if they were selected?

You're returning everything together.

Thank you

    
asked by anonymous 18.10.2018 / 18:50

1 answer

1

What you need is to use the array function join .

See what's in this example: link

HTML:

<select id="estado" multiple>
<option>SP</option>
<option>RJ</option>
<option>ES</option>
<option>MG</option>
</select>

JavaScript code with your request:

const estados = $('#estado').val().join(', '); //Resultado: SP, RJ, ES
    
18.10.2018 / 20:19