javascript - Check if all dropdowns in page has a selected option -


need enable button on page if questions have been answered, have 5 dropdowns , need make sure user answer of them before letting him go next step.

tried iterate through each select on each change, can't make work, bad js knowledge.

 arr = $('.select1, .select2, .select3, .select4, .select5')   $('.select1, .select2, .select3, .select4, .select5').on('change', function(event) {     $.each(arr, function(index, val) {        /* tried here */     });   }); 

i wondering may there better way this, in 1 line: select.selected.size = 5.

thank kind of - ideas, solutions, links, etc..

i had few moments, thought i'd offer potential solution; given html following:

<select name="test1">     <option value="-1">please select</option>     <option value="1">1</option>     <option value="2">2</option>     <option value="3">3</option> </select> <!-- html repeated as --> 

the following plug-in work:

(function($){     $.fn.allselected = function(){         return this.filter(function(){             return parseint(this.value, 10) > 0;         }).length >= this.length;     }; })(jquery) 

and can called so:

$('select').on('change', function(){     console.log($('select').allselected()); }); 

js fiddle demo.

updated above value can passed plug-in considered invalid choice (in demo i've, arbitrarily, used 3), defaults -1 if user supplies no argument. in case if any of elements have value equal nochoicevalue (for want of better name) plug-in returns false:

(function($){     $.fn.allselected = function(opts){         var s = $.extend({             'nochoicevalue' : -1         }, opts);         return this.filter(function(){             return this.value !== s.nochoicevalue.tostring();         }).length >= this.length;     }; })(jquery);  $('select').on('change', function(){     console.log($('select').allselected({         'nochoicevalue' : 3     })); }); 

js fiddle demo.

references:


Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -