Andrew
Andrew

Reputation: 111

Retrieving the array of selected values in select2

I have the following issue where i cannot retrieve the selected array from select2 in my JS.

<select class="js-example-basic-multiple"
        name="states[]"
        multiple="multiple"
        style="width:500px;">

  <option>"dynamic value"</option>
  <option>"dynamic value2"</option>

And the associated script:

var caseCodes = $('.js-example-basic-multiple option:selected').text()

The above javascript function retrieves all my selected values as a single long string.

Is there any way i can retrieve the array and loop through it to find each individual selected value?

Upvotes: 1

Views: 166

Answers (1)

Jeremy Thille
Jeremy Thille

Reputation: 26360

Just read the <select>'s value :

$('.js-example-basic-multiple').val()

Upvotes: 3

Related Questions