Dev1997
Dev1997

Reputation: 677

JQuery selector check the number of checkboxes has been checked

What I'm trying to is run some code if the number of input[type="checkbox"] is checked equal to the value of 2 or 3.

Here is an example of my JQuery:

if ($('input:checkbox:checked').length == 2 or 3) {
  // run some code
}

Im not sure how to program it to understand whether the value is 2 or 3.

Any ideas?

Upvotes: 0

Views: 72

Answers (2)

Xavier J
Xavier J

Reputation: 4624

Please read up on Javascript logical operators. This is basic stuff.

Logical operators are used to determine the logic between variables or values.

Given that x = 6 and y = 3, the table below explains the logical operators:

Operator      Description      Example  
========================================================
&&            and              (x < 10 && y > 1) is true    
||            or               (x === 5 || y === 5) is false    
!             not              !(x === y) is true

Upvotes: 0

Duly Kinsky
Duly Kinsky

Reputation: 996

Use the or operator ||

if ($('#id').length === 2 || $('#id').length === 3) {
  // run some code
}

Upvotes: 1

Related Questions