1

I have 7 simple select boxes:

<select>
  <option selected disabled>choose something</option>
  <option>some text</option>
  <option>some more text</option>
  <option>and more and more</option>
</select>

<select>
  <option selected disabled>choose something</option>
  <option>some text</option>
  <option>some more text</option>
  <option>and more and more</option>
</select>

<select>
  <option selected disabled>choose something</option>
  <option>some text</option>
  <option>some more text</option>
  <option>and more and more</option>
</select>

<select>
  <option selected disabled>choose something</option>
  <option>some text</option>
  <option>some more text</option>
  <option>and more and more</option>
</select>

How can I check if ALL the select boxes have something selected rather than the default option?

Mohammad
  • 21,175
  • 15
  • 55
  • 84
Alexandru Vlas
  • 1,355
  • 3
  • 18
  • 30
  • where is option value? – yash Sep 13 '16 at 07:36
  • Set the `value` of the `select` input.... If value attribute is not specified, `text` will be treated as `value`.. – Rayon Sep 13 '16 at 07:36
  • You get the Element in any number of ways, such as `document.getElemnetById('HTMLidAttribute')` then it could be as simple as testing for `Element.selected`. – StackSlave Sep 13 '16 at 07:37
  • then you can check that via jquery at [here](http://stackoverflow.com/a/15987166/5813861) – yash Sep 13 '16 at 07:38

3 Answers3

3

You can find selected option that are disabled, if length==0 then no default element are selected.

if($('option[disabled]:selected').length == 0){
   // ALL the select boxes have something selected rather than the default option
}
Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125
2

Try this. Get value of select and check if its null and increment the count if its not.

$(function(){
    $('#btn').click(function(){
    var count = 0;

    $('select').each(function(){
      
    if($(this).val() != null){
       count ++; 
     }
     
  })
    if(count == 4) {
      console.log('all selected');
    }
  })
  
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select>
  <option selected disabled>choose something</option>
  <option>some text</option>
  <option>some more text</option>
  <option>and more and more</option>
</select>

<select>
  <option selected disabled>choose something</option>
  <option>some text</option>
  <option>some more text</option>
  <option>and more and more</option>
</select>

<select>
  <option selected disabled>choose something</option>
  <option>some text</option>
  <option>some more text</option>
  <option>and more and more</option>
</select>

<select>
  <option selected disabled>choose something</option>
  <option>some text</option>
  <option>some more text</option>
  <option>and more and more</option>
</select>

<button id="btn">Check</button>
Shubham Khatri
  • 270,417
  • 55
  • 406
  • 400
0

This works for me

            var count = 0;
            $("select").each(function() {
                if(this.value === ''){
                    count++
                }
            });
            if (count === 0) {
                // all select boxes has selected option 
            }
Altravista
  • 357
  • 3
  • 12