javascript - Count Unique Selection from Multiple Dropdown -
i'm new jquery, i'm working on survey form , have multiple dropdown menus different questions have same dropdown value. supposed have:
<select name="forms[agentispitch]" id="forms_agentispitch"> <option value="">choose one</option> <option value="yes">yes</option> <option value="no">no</option> <option value="n/a">n/a</option> </select> <select name="forms[mandatoryoptisstated]" id="forms_mandatoryoptisstated"> <option value="">choose one</option> <option value="yes">yes</option> <option value="no">no</option> <option value="n/a">n/a</option> </select> and other different dropdowns different id's. best way count how many has selected yes, no , n/a/ ? thanks
you can simple way
$('select').change(function() { // selects var allselects = $('select'); // set values count type var yes = 0; var no = 0; // each select increase count $.each(allselects, function(i, s) { // increase count if($(s).val() == 'yes') { yes++; } if($(s).val() == 'no') { no++; } }); // update count values summary $('.cnt-yes').text(yes); $('.cnt-no').text(no); });
Comments
Post a Comment