Get a list of selected values ​​in a ListBox

You already have a question with tons of votes for choosing a selected value from the drop-down list using jQuery here .

This answer almost works for a list, but if multiple values ​​are selected, the result will be a single row with all concatenated values. This is not useful. I need a collection (list, array, independently) of text values ​​for each selected option.

At the moment, I think that I will use most of the answer from this other question, but without .text() at the end, and then repeat all matches. Best ideas?

+6
source share
2 answers

You can make multiple selected text using an iteration loop as follows.

 $('#f1').click(function(){ var rr = []; $('.selectpicker :selected').each(function(i, selected){ rr[i] = $(selected).text(); }); alert(rr); }); 

OR, if you want to use its value , just write.

 $('.selectpicker').val(); 

Demo

+10
source

You can use as below:

 var selectedVal= []; $('#multiple :selected').each(function(i, selected){ selectedVal[i] = $(selected).text(); alert(selectedVal[i]); }); 

Here is a tutorial and example

+1
source

Source: https://habr.com/ru/post/982241/


All Articles