0

I have select list

 <select name="select-choice-min" id="mainMenu" data-mini="true">
      <option value="10">10</option>
      <option value="11">11</option>
      ...
 </select>

I tried with

var arr = new Array();
var sval = $("#mainMenu option").val();
arr.push(sval);

but on alerting arr variable I'm getting only first value 10, 10, 10, ...

so, how to get all select option values?

4 Answers 4

6

Try this

Loop through #mainMenu option using .each() then get appropraite values using $(this).val() then push using arr.push($(this).val());

var arr = new Array();
$('#mainMenu option').each(function(){
arr.push($(this).val());
});
console.log(arr)

Working DEMO

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

Comments

5

You can use a .map() like below to create an array of values

var arr = $("#mainMenu option").map(function(){
    return $(this).val()
}).get();

Comments

2

You can use .map(), which will create an array of all option values.

var arr = $('#mainMenu option').map(function(){
    return this.value;
}).get();

Comments

1

Try to use .map() along with .get() to accomplish your task,

var arr = $("#mainMenu option").map(function(){ return this.value; }).get();

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.