Search code examples
jquerycheckboxjquery-ui-widget

jQuery - Correctly displaying checked count number


I'm using Eric Hynds jQuery UI MultiSelect Widget and trying to only display the check boxes from the widget itself. The "normal" checkboxes should not affect the count. Currently, the normal check boxes add to the count if you check one, then check one from the widget...resulting in count=2.

Please see my fiddle: http://jsfiddle.net/PdLBX/6/

$(document).ready(function () {
        $(".multiselect").each(function () {
          $(this).change(updateCount);
        });
        updateCount();
        function updateCount () {
          var count = $("input[type=checkbox]:checked").size();
          $("#count").text(count);
        };

      });

I've tried something like this but no luck.

 function updateCount () {
          var count1 = $("[id^=ui-multiselect-dropdown1]:checked").size(),
              count2 = $("[id^=ui-multiselect-dropdown2]:checked").size(); 
          $("#count").text(count1+count2);
        };

Solution

  • Use .length

    function updateCount () {
      var count1 = $("[id^=ui-multiselect-dropdown1]:checked").length,
          count2 = $("[id^=ui-multiselect-dropdown2]:checked").length; 
      $("#count").text(count1+count2);
    };
    

    Documentation Here