10

HTML CODE

<select class="form-control" name="min_select[]">
              <option value="15">15</option>
              <option value="30">30</option>
</select>

JQuery Code

var val1[];
$('select[name="min_select[]"] option:selected').each(function() {
val1.push($(this).val());
});

when i run this code I get empty val array

1
  • is this a select with multiple attribute? Commented Jan 27, 2016 at 4:50

4 Answers 4

12

This will also work

var  val1= $("select[name=\'min_select[]\']").map(function() {
    return $(this).val();
}).toArray();
Sign up to request clarification or add additional context in comments.

1 Comment

That it is, what I was looking for.
7

The declaration array syntax is in correct.Please check the below code

 var val1=[]; 
 $('select[name="min_select[]"] option:selected').each(function() {
  val1.push($(this).val());
 });

Comments

1

You can try the following

HTML

<select class="form-control min-select" name="min_select[]">
          <option value="15">15</option>
          <option value="30">30</option>
</select>

JQUERY

var values = [];
$("select.min-select").each(function(i, sel){
    var selectedVal = $(sel).val();
    values.push(selectedVal);
});

Is min_select[] a multiple choice select?

1 Comment

min_select[] is not a multiple select.
1

To get the selected value, whether it is multiselect or single select, use jQuery .val() method.

If it is a multiselect, it will return an array of the selected values.

See jsfiddle for demo. Check console log

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.