4

How can I get the values selected in the drop-down list, using a JavaScript function? User can select multiple values from both the elements. Following are the elements I'm using. Thanks in advance.

<select name="icOptions" id="icOptions" style="display: none" multiple="multiple">
  <option value="Choose an Option" selected="selected">Choose a Team </option>
  <option value="IDX">IDX</option>
  <option value="Support">SUPPORT</option>
  <option value="webapps">WEBAPPS</option>
</select>

<select name="ocOptions" id="ocOptions" style="display: none" multiple="multiple">
  <option value="Choose an Option" selected="selected">Choose a TeamMember </option>
  <option value="sanjay740">sanjay740</option>
  <option value="milind740">milind740</option>
</select>

3 Answers 3

13
var fld = document.getElementById('icOptions');
var values = [];
for (var i = 0; i < fld.options.length; i++) {
  if (fld.options[i].selected) {
    values.push(fld.options[i].value);
  }
}
// do something with values
Sign up to request clarification or add additional context in comments.

1 Comment

Thanx it worked with little modification. alert('you selected' + fld.options[i].value); because i wanted selected values. well Thanx again
0

Really Awesome Library for your need using jQuery or Prototype http://harvesthq.github.com/chosen/ Have a look at it.

It supports select & multiselect in a really nice way

Comments

0

An ES6/functional style alternative to the accepted answer:

const values = Array.apply(null, e.target.options)
  .filter(option => option.selected)
  .map(option => option.value);

1 Comment

While this code may answer the question, it's usually better to provide an explanation of why and how this code helps in this case. See How do I write a good answer

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.