0

I'm trying to have JavaScript count one set of checkboxes and not count a set of checkboxes.

Right now I have the following JavaScript code:

function updateCount {
    var count = $("input[type=checkbox]:checked").size();
    $("#count").text(count);    
};
});

Here are my checkboxes:

<input type="checkbox" name="checkbox_1">Checkbox 1
<input type="checkbox" name="checkbox_2">Checkbox 2

So basically with my JavaScript is there a way to only count the first checkbox but not the second checkbox.

j08691
  • 204,283
  • 31
  • 260
  • 272
Kevin Davis
  • 367
  • 1
  • 7
  • 25
  • 1
    Your code is incomplete. How do you call your `updateCount` function, and you have an extra `});`. You should also be using `.length` instead of `.size()`. – j08691 Mar 16 '16 at 20:46
  • You should use `.length`, not `size()`. See [this answer](http://stackoverflow.com/questions/14202601/array-size-vs-array-length). – Ionian316 Mar 16 '16 at 20:46
  • If you count only the first checkbox the result will always be one? Not sure what you meant... – Pedro Villa Verde Mar 16 '16 at 20:47
  • @Ionian316 `size()` is a perfectly valid method of a jQuery object in this case, although I agree, length is better practice. – Rory McCrossan Mar 16 '16 at 20:47
  • are you asking how to count how many checkboxes are checked? or are you asking how to count one set of checkboxes (given some other condition) from the whole group of checkboxes? – indubitablee Mar 16 '16 at 20:48

3 Answers3

1

If you're able to modify the HTML of your checkboxes, I would group them using a class.

<input class="countThis" type="checkbox" name="checkbox_1">Checkbox 1
<input class="countThis" type="checkbox" name="checkbox_2">Checkbox 2

And just leave that class off anything you don't want to count

<input type="checkbox" name="checkbox_3">Checkbox 3
<input type="checkbox" name="checkbox_4">Checkbox 4

And finally, your function would just look like this

function updateCount {
    var count = $(".countThis:checked").size();
    $("#count").text(count);    
};
sm1215
  • 437
  • 2
  • 14
0

There are two easy ways to do this.

First, if you want to pick which checkbox to count:

$('input[type=checkbox][name="checkbox_1"]').length

Second, if you want to pick which checkbox to exclude:

$('input[type=checkbox][name!="checkbox_2"]').length

TAGraves
  • 1,369
  • 1
  • 9
  • 11
  • idk how you can give an answer when the question isnt clear – indubitablee Mar 16 '16 at 20:49
  • I found the question clear enough; Kevin is obviously trying either to figure out how to select only some checkboxes, or to avoid selecting only some checkboxes. I provided the two easiest (though perhaps not the fastest) ways to do that. If Kevin's plans are more complicated than he has let on, then he can ask for clarification here, but the two strategies I outlined are going to be the ones he wants to use regardless of how complicated his selectors end up being. – TAGraves Mar 16 '16 at 20:51
  • without knowing what criteria op wants to use to differentiate the chkbxs to select and not, you cant sufficiently answer the question. his question as is: how do i select some chkbxs and not others? your answer is: select the ones you want or exclude the ones you dont want. yes, of course he wants to select those he wants to select and not select the others. op wants to catch some fish, so you say use a fishing pole, but you fail to ask for clarification on what type of fish he wants to catch, which determines what type of bait to use. what selectors to use is the answer he's looking for. – indubitablee Mar 16 '16 at 21:17
0

If you always need just the first element you can do it using :first-of-type selector:

$( "input[type='checkbox']:first-of-type" ).prop({
checked: true
});
Tarek.hms
  • 1,243
  • 1
  • 10
  • 15