views:

960

answers:

5

I've written some JQuery code to iterate over some checkboxes on an ASP.Net web page, and am seeing 2 very strange problems with the if statement embedded in the $.each function. The code is selecting my checkboxes just fine, and as the code iterates through the $.each function I can clearly see that the objValue.checked is true or false, appropriately, depending on if I checked the checkbox or not. The problems that I am seeing are: 1) that it always falls into my if statement (even when the value of objValue.checked is false), and 2) the value of my availableInstalls variable is never decremented, even though I can see in debug mode that it hits the "availableInstalls = availableInstalls - 1" line of code. Anyone know what may be causing this behavior?

      $(document).ready(function() {
          $(".installsoftwarecb").click(function() {

              var availableInstalls = 10;

              var checkBoxes = $(".installsoftwarecb input[type='checkbox']");
              $.each(
              checkBoxes, function(intIndex, objValue) {

                  if (objValue.checked) {
                      availableInstalls = availableInstalls - 1;
                  }
              });
          });
      });
A: 

Try being specific in your code. Instead of if (objValue.checked) write if (ojValue.checked == true)

xav0989
if the checkbox is checked objValue.checked is true. Adding == true is just extra code that is not required.
nickyt
+3  A: 

How about writing something like this:

  $(document).ready(function() 
  {
    $(".installsoftwarecb").click(function() 
    {
        //count number of checked items directly. 
        var availableInstalls = 10 -  $('input:checkbox:checked', this).length;
        alert(availableInstalls);
    }
  }
SolutionYogi
The selector is going to work, but it should really be input:checkbox:checked instead, :checked is going to look at every single element in the context which can be pretty inefficient. You can filter it down to checkboxes with input:checkbox - it is probably not a big deal most of the time but it is just good practice.
Paolo Bergantino
As I wrote in the other comment, I agree with you and I have edited my answer. Personally, I don't think speed wise it would really matter but the fact that it can also count radio button element is a potential for bug when the HTML gets modified in the future.
SolutionYogi
Thank you for updating your answer. +1 :)
Paolo Bergantino
Well, speed wise it won't matter MOST of the time, but it is a good habit. I mean if ".installsoftwarecb" happens to be a rather large div with 100 elements where only 10 are checkboxes there's really no point in iterating through 100 elements seeing if they're checked. Is your user's browser going to die? No. But there's a better, equally simple alternative so there's no point in not using it.
Paolo Bergantino
A: 

That's really strange that it would fall into your conditional block even if the condition was false. You could try only selecting the checked items to begin with:

var checkBoxes = $(".installsoftwarecb input[type='checkbox']:checked");

http://docs.jquery.com/Selectors/checked

Nick Riggs
+2  A: 

Working Demo

code from demo

  $(document).ready(function() {
      $("#button").click(function() {

          var availableInstalls = 10;

          $("input:checkbox").each(function() {

              if (this.checked) {
                  availableInstalls = availableInstalls - 1;
              }
          });
          alert(availableInstalls);
       });
  });

or you cold just use a "checked" selector

 $("input:checkbox:checked").length; // number of checked checkboxes
Russ Cam
You should not do :checkbox alone as it is equivalent to *:checkbox, you should always prefix it with input
Paolo Bergantino
Updated now. Cheers
Russ Cam
Thanks. +1
Paolo Bergantino
A: 

I think you should be using jQuery.each rather than $().each. ( http://docs.jquery.com/Utilities/jQuery.each ). $().each does not take a callback, where jQuery.each does.

Brian