Dave's Brain

Browse - Programming Tips - How can I get all values in a multiple select?

Date: 2016dec19
Language: javaScript
Library: jQuery

Q.  How can I get all values in a multiple select?

A.  With jQuery its easy but with one caveat.

<select id=mysel multiple size=5> <option>One</option> <option>Two</option> <option>Three</option> <option>Four</option> <option>Five</option> <option>Six</option> <option>Seven</option> </select> <script> var arr = $('#mysel').val(); // Nicely jQuery returns an array with an entry for each selected value if (arr == null) arr = []; // If nothing is select, you get null so I set it to an empty array console.log('There are ' + arr.length + ' things selected'); </script>

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.