1

I can only get the last selected value on my select multiple dropdown.

I have tried answers on the forum but no luck.

My HTML:

<select id="provider" class="validate" multiple required>
  <option value="" disabled>Invoice Provider Name</option>
  <option>Provider 1</option>
  <option>Provider 2</option>
</select>

My JS:

function addRow(){
  var x=document.getElementById("provider");
  var pro = '';
  for (var i = 0; i < x.options.length; i++) {
    if(x.options[i].selected ==true){
      pro = x.options[i].length.value;
    }
  }
}

assuming that I selected Provider 1, Provider 2 on the dropdown, I want to get

Provider 1
Provider 2

Thank you.

2
  • 1
    Please add the HTML select element as well. As for your problem, pro is a 'string' type variable. It can store only one value which gets over written. Commented Aug 6, 2019 at 4:55
  • Added HTML. How can I made pro to hold multiple values? Commented Aug 6, 2019 at 4:59

1 Answer 1

2

You can try with Array.prototype.filter():

The filter() method creates a new array with all elements that pass the test implemented by the provided function.

And Array.prototype.map():

The map() method creates a new array with the results of calling a provided function on every element in the calling array.

Try the following way:

document.getElementById('provider').addEventListener('change', function(){
  console.clear();
  let selected = [...this.options].filter(option => option.selected).map(o => o.value);
  console.log(selected);
});
<select id="provider" class="validate" multiple required>
  <option value="" disabled>Invoice Provider Name</option>
  <option>Provider 1</option>
  <option>Provider 2</option>
</select>

In your way by declaring an array and pushing option's value to the array if selected:

function addRow(el){
  var x = el;
  var pro = []; // create an array
  for (var i = 0; i < x.options.length; i++) {
    if(x.options[i].selected == true){
      pro.push(x.options[i].value); // push the value to the array
    }
  }
  console.log(pro);
}
<select id="provider" class="validate" multiple required onchange="addRow(this)">
    <option value="" disabled>Invoice Provider Name</option>
    <option>Provider 1</option>
    <option>Provider 2</option>
</select>

Sign up to request clarification or add additional context in comments.

12 Comments

I wanna get specific select multiple dropdown via ID. Is this possible with your code?
Instead of using document.querySelector('select'), you can use document.querySelector('#provider')
@RJVillamer, like the previous comment from Krishna Prashatt, updated the answer, please check:)
I suggest changing query selector to getElementById as it's much faster. And changing o.text to o.value
:) I would upvote again if I could. I would also recomment not using this in that function. Instead I would change it to .addEventListener('change', ev => { const sel = ev.target; ...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.