Leandro Garcia
Leandro Garcia

Reputation: 3228

get options values on multiple select

JS

function getLandmarks(places, stores) {
  console.log(places);
}

HTML

<select name="places[]" id="places" onchange="getLandmarks(this.value, '');" multiple="multiple">
<option value="1">Place 1</option>
<option value="2">Place 2</option>
<option value="3">Place 3</option>
<option value="4">Place 4</option>
</select>

Based on the above, I am having trouble in getting the value of multiple items I selected. If I choose "Place 1", the console.log displays 1 which is correct. However, if I do a multiple selection the value of the first option that I selected is the one I always get. How can I get at least an array of the option's values? Or do I need to jQuery for this?

Upvotes: 0

Views: 699

Answers (1)

Arun P Johny
Arun P Johny

Reputation: 388316

You can use .val()

<select name="places[]" id="places" onchange="getLandmarks($(this).val(), '');" multiple="multiple">
<option value="1">Place 1</option>
<option value="2">Place 2</option>
<option value="3">Place 3</option>
<option value="4">Place 4</option>
</select>

Demo: Fiddle

Upvotes: 2

Related Questions