18
    <!DOCTYPE html>
    <html>
    <script>
    function getValue()
    {
      var x = document.getElementById("sel");
      for (var i = 0; i < x.options.length; i++) {
          if (x.options[i].selected == true) {
              alert(x.options[i].selected);
          }
      }
    }
    </script>
    </head>
    <body>
    <select multiple="multiple" id="sel">
      <option value="volvo">Volvo</option>
      <option value="saab">Saab</option>
      <option value="opel">Opel</option>
      <option value="audi">Audi</option>
    </select>
    <input type="button" value="Get Value" onclick="getValue()"/>
    </body>
    </html>

This is my code. How do I get all the selected values from a listbox using JavaScript? The above code is showing true for all the selected values.

0

7 Answers 7

18

Replace

  if(x.options[i].selected ==true){
      alert(x.options[i].selected);
  }

with

  if(x.options[i].selected){
      alert(x.options[i].value);
  }
Sign up to request clarification or add additional context in comments.

1 Comment

It's useless to use if(x.options[i].selected==true) instead of if(x.options[i].selected), but it could make sense if(x.options[i].selected===true) or if(!!x.options[i].selected)
6

I suggest that you do it using jQuery like this:

var selected = $('#sel option:selected');

If you prefer not to use jQuery then disregard this answer and refer to the other ones.

3 Comments

Question asks for a JavaScript solution and is not tagged jQuery
@Phil Just making a suggestion, the user can disregard it if he doesn't use jQuery, but also it may present to him easy DOM access he may consider.
From api.jquery.com/selected-selector: "Because :selected is a jQuery extension and not part of the CSS specification, queries using :selected cannot take advantage of the performance boost provided by the native DOM querySelectorAll() method. To achieve the best performance when using :selected to select elements, first select the elements using a pure CSS selector, then use .filter(":selected")". Therefore var selected = $('#sel option').filter(':selected'); is better.
3

use selectedOptions

for (var i = 0; i < x.selectedOptions.length; i++) {
    alert(x.selectedOptions[i].value);
}

Comments

0

Solution with the same example

    <!DOCTYPE html>
    <html>
    <script>
    function getValue()
    {
        var x = document.getElementById("sel");
        for (var i = 0; i < x.options.length; i++) {
            if (x.options[i].selected == true) {
                alert(x.options[i].value);
            }
        }
    }
    </script>
    </head>
    <body>
    <select multiple="multiple" id="sel">
      <option value="volvo">Volvo</option>
      <option value="saab">Saab</option>
      <option value="opel">Opel</option>
      <option value="audi">Audi</option>
    </select>
    <input type="button" value="Get Value" onclick="getValue()"/>
    </body>
    </html>

Comments

0

In modern browsers that support ECMAScript 2015 you can use

let selectedValues = Array.from(multiSelectElement.options).filter(o=>o.selected).map(o=>o.value)

or (using the spread operator)

let selectedValues = [...multiSelectElement.options].filter(o=>o.selected).map(o=>o.value)

For antique browsers like Internet Explorer and for extreme backwards compatibility you can still do it the long way:

var selectedValues = [];
for (var i = 0; i < multiSelectElement.options.length; ++i) {
    var option = multiSelectElement.options[i];
    if (option.selected) {
        selectedValues.push(option.value);
    }
}

where multiSelectElement is a reference to the select element (use document.getElementById("sel") or whatever)

Reference: Most efficient way to convert an HTMLCollection to an Array

Full example:

function getValues() {
  let multiSelectElement = document.getElementById("sel");
  let selectedValues = Array.from(multiSelectElement.options).filter(o => o.selected).map(o => o.value);
  console.log(selectedValues);
}
<!DOCTYPE html>
<html>
<body>
  <p>Windows desktop browsers: Use CTRL+Click or SHIFT+Click to select multiple options
  </p>
  <select multiple id="sel">
    <option value="volvo">Volvo</option>
    <option value="saab">Saab</option>
    <option value="opel">Opel</option>
    <option value="audi">Audi</option>
  </select>
  <input type="button" value="Get Values" onclick="getValues()" />
</body>
</html>

Comments

0

your question is very simple replace this function with your old function

function getValue() {
        var x = document.getElementById("sel");
        for (var i = 0; i < x.options.length; i++) {
            if (x.options[i].selected == true) {
                alert(x.options[i].value);
            }
        }

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
-1
var obj = document.querySelectorAll('#sel option:checked');
var result = Object.keys(obj).map((key) => [obj[key].value, obj[key].text]);
alert(result);

The above is jQuery solution, in case somebody like me searches for javascript solution but decides to go with jQuery.

Comments

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.