Jquery count selection-boxes that have an item selected

javascript, jquery

Solution

This will return the number of selected items that is not empty as you wished:

function getCount(){
   return $("select.jquery-countable option:selected[value!='']").length;
}

Problem

i wonder if there is a more efficient way of counting the selections of a user: This is the selection dropdown that is multiple times on the page: ``` <select class="span2 jquery-countable" name="category[7]"> <option></option> <option value="1">1. vote</option> <option value="2">2. vote</option> <option value="3">3. vote</option> <option value="4">4. vote</option> </select> ``` This is my js that counts the selections: ``` $(document).ready(function () { $('#selected-count').html('You choose ' + getCount() + ' entries'); function getCount() { prio1 = $('.jquery-countable option:selected[value="1"]').length; prio2 = $('.jquery-countable option:selected[value="2"]').length; prio3 = $('.jquery-countable option:selected[value="3"]').length; prio4 = $('.jquery-countable option:selected[value="4"]').length; return prio1 + prio2 + prio3 + prio4; } $('.jquery-countable').change(function () { $('.jquery-countable option:selected').each(function () { $('#selected-count').html('You choose ' + getCount() + ' entries'); }) }) }); ``` My goal is it to count all selections that where made by the user that are not empty?! Is there a more efficient way? Thanks!

Original source