count the active classes on both choices












0















I'm working on 2 selections, that adds active class on click and add disabled class on the opposite selection. My problem is how get the count of classes exist on both selection ex. selection1 = 1 and selection2 = 5. Also my counting and display of result is off.



Hope you help me thanks.






$('.numChoice > .col-md-5ths > .num').on("click", function() {
var idx = $(this).parent().index();
var act = $(this).is(".active");
var $otherSide = $(this).closest(".numChoice").siblings(); // plural but only one
var $otherElement = $otherSide.find(".col-md-5ths").eq(idx).children(":first");
$(this).toggleClass('active');
$otherElement.toggleClass('disabled', !act);

var len1 = $otherSide.find('.active').length;
var len2 = $(this).closest('.numChoice').find('.active').length;
console.log('selection1: ' + len1, 'selection2: ' + len2);

});

.numChoice {
display: block;
}

.num {
width: 20px;
height: 20px;
background-color: #DDD;
text-align: center;
float: left;
cursor: pointer;
}

.num.active {
color: green;
}

.num.disabled {
color: red;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<div class="numChoice first-row">
<div class="col-md-5ths">
<div class="num">1</div>
</div>
<div class="col-md-5ths">
<div class="num">2</div>
</div>
<div class="col-md-5ths">
<div class="num">3</div>
</div>
<div class="col-md-5ths">
<div class="num">4</div>
</div>
<div class="col-md-5ths">
<div class="num">5</div>
</div>
</div>
<br><br>
<div class="numChoice second-row">
<div class="col-md-5ths">
<div class="num">1</div>
</div>
<div class="col-md-5ths">
<div class="num">2</div>
</div>
<div class="col-md-5ths">
<div class="num">3</div>
</div>
<div class="col-md-5ths">
<div class="num">4</div>
</div>
<div class="col-md-5ths">
<div class="num">5</div>
</div>
</div>
</div>












share|improve this question



























    0















    I'm working on 2 selections, that adds active class on click and add disabled class on the opposite selection. My problem is how get the count of classes exist on both selection ex. selection1 = 1 and selection2 = 5. Also my counting and display of result is off.



    Hope you help me thanks.






    $('.numChoice > .col-md-5ths > .num').on("click", function() {
    var idx = $(this).parent().index();
    var act = $(this).is(".active");
    var $otherSide = $(this).closest(".numChoice").siblings(); // plural but only one
    var $otherElement = $otherSide.find(".col-md-5ths").eq(idx).children(":first");
    $(this).toggleClass('active');
    $otherElement.toggleClass('disabled', !act);

    var len1 = $otherSide.find('.active').length;
    var len2 = $(this).closest('.numChoice').find('.active').length;
    console.log('selection1: ' + len1, 'selection2: ' + len2);

    });

    .numChoice {
    display: block;
    }

    .num {
    width: 20px;
    height: 20px;
    background-color: #DDD;
    text-align: center;
    float: left;
    cursor: pointer;
    }

    .num.active {
    color: green;
    }

    .num.disabled {
    color: red;
    }

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="container">
    <div class="numChoice first-row">
    <div class="col-md-5ths">
    <div class="num">1</div>
    </div>
    <div class="col-md-5ths">
    <div class="num">2</div>
    </div>
    <div class="col-md-5ths">
    <div class="num">3</div>
    </div>
    <div class="col-md-5ths">
    <div class="num">4</div>
    </div>
    <div class="col-md-5ths">
    <div class="num">5</div>
    </div>
    </div>
    <br><br>
    <div class="numChoice second-row">
    <div class="col-md-5ths">
    <div class="num">1</div>
    </div>
    <div class="col-md-5ths">
    <div class="num">2</div>
    </div>
    <div class="col-md-5ths">
    <div class="num">3</div>
    </div>
    <div class="col-md-5ths">
    <div class="num">4</div>
    </div>
    <div class="col-md-5ths">
    <div class="num">5</div>
    </div>
    </div>
    </div>












    share|improve this question

























      0












      0








      0








      I'm working on 2 selections, that adds active class on click and add disabled class on the opposite selection. My problem is how get the count of classes exist on both selection ex. selection1 = 1 and selection2 = 5. Also my counting and display of result is off.



      Hope you help me thanks.






      $('.numChoice > .col-md-5ths > .num').on("click", function() {
      var idx = $(this).parent().index();
      var act = $(this).is(".active");
      var $otherSide = $(this).closest(".numChoice").siblings(); // plural but only one
      var $otherElement = $otherSide.find(".col-md-5ths").eq(idx).children(":first");
      $(this).toggleClass('active');
      $otherElement.toggleClass('disabled', !act);

      var len1 = $otherSide.find('.active').length;
      var len2 = $(this).closest('.numChoice').find('.active').length;
      console.log('selection1: ' + len1, 'selection2: ' + len2);

      });

      .numChoice {
      display: block;
      }

      .num {
      width: 20px;
      height: 20px;
      background-color: #DDD;
      text-align: center;
      float: left;
      cursor: pointer;
      }

      .num.active {
      color: green;
      }

      .num.disabled {
      color: red;
      }

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <div class="container">
      <div class="numChoice first-row">
      <div class="col-md-5ths">
      <div class="num">1</div>
      </div>
      <div class="col-md-5ths">
      <div class="num">2</div>
      </div>
      <div class="col-md-5ths">
      <div class="num">3</div>
      </div>
      <div class="col-md-5ths">
      <div class="num">4</div>
      </div>
      <div class="col-md-5ths">
      <div class="num">5</div>
      </div>
      </div>
      <br><br>
      <div class="numChoice second-row">
      <div class="col-md-5ths">
      <div class="num">1</div>
      </div>
      <div class="col-md-5ths">
      <div class="num">2</div>
      </div>
      <div class="col-md-5ths">
      <div class="num">3</div>
      </div>
      <div class="col-md-5ths">
      <div class="num">4</div>
      </div>
      <div class="col-md-5ths">
      <div class="num">5</div>
      </div>
      </div>
      </div>












      share|improve this question














      I'm working on 2 selections, that adds active class on click and add disabled class on the opposite selection. My problem is how get the count of classes exist on both selection ex. selection1 = 1 and selection2 = 5. Also my counting and display of result is off.



      Hope you help me thanks.






      $('.numChoice > .col-md-5ths > .num').on("click", function() {
      var idx = $(this).parent().index();
      var act = $(this).is(".active");
      var $otherSide = $(this).closest(".numChoice").siblings(); // plural but only one
      var $otherElement = $otherSide.find(".col-md-5ths").eq(idx).children(":first");
      $(this).toggleClass('active');
      $otherElement.toggleClass('disabled', !act);

      var len1 = $otherSide.find('.active').length;
      var len2 = $(this).closest('.numChoice').find('.active').length;
      console.log('selection1: ' + len1, 'selection2: ' + len2);

      });

      .numChoice {
      display: block;
      }

      .num {
      width: 20px;
      height: 20px;
      background-color: #DDD;
      text-align: center;
      float: left;
      cursor: pointer;
      }

      .num.active {
      color: green;
      }

      .num.disabled {
      color: red;
      }

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <div class="container">
      <div class="numChoice first-row">
      <div class="col-md-5ths">
      <div class="num">1</div>
      </div>
      <div class="col-md-5ths">
      <div class="num">2</div>
      </div>
      <div class="col-md-5ths">
      <div class="num">3</div>
      </div>
      <div class="col-md-5ths">
      <div class="num">4</div>
      </div>
      <div class="col-md-5ths">
      <div class="num">5</div>
      </div>
      </div>
      <br><br>
      <div class="numChoice second-row">
      <div class="col-md-5ths">
      <div class="num">1</div>
      </div>
      <div class="col-md-5ths">
      <div class="num">2</div>
      </div>
      <div class="col-md-5ths">
      <div class="num">3</div>
      </div>
      <div class="col-md-5ths">
      <div class="num">4</div>
      </div>
      <div class="col-md-5ths">
      <div class="num">5</div>
      </div>
      </div>
      </div>








      $('.numChoice > .col-md-5ths > .num').on("click", function() {
      var idx = $(this).parent().index();
      var act = $(this).is(".active");
      var $otherSide = $(this).closest(".numChoice").siblings(); // plural but only one
      var $otherElement = $otherSide.find(".col-md-5ths").eq(idx).children(":first");
      $(this).toggleClass('active');
      $otherElement.toggleClass('disabled', !act);

      var len1 = $otherSide.find('.active').length;
      var len2 = $(this).closest('.numChoice').find('.active').length;
      console.log('selection1: ' + len1, 'selection2: ' + len2);

      });

      .numChoice {
      display: block;
      }

      .num {
      width: 20px;
      height: 20px;
      background-color: #DDD;
      text-align: center;
      float: left;
      cursor: pointer;
      }

      .num.active {
      color: green;
      }

      .num.disabled {
      color: red;
      }

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <div class="container">
      <div class="numChoice first-row">
      <div class="col-md-5ths">
      <div class="num">1</div>
      </div>
      <div class="col-md-5ths">
      <div class="num">2</div>
      </div>
      <div class="col-md-5ths">
      <div class="num">3</div>
      </div>
      <div class="col-md-5ths">
      <div class="num">4</div>
      </div>
      <div class="col-md-5ths">
      <div class="num">5</div>
      </div>
      </div>
      <br><br>
      <div class="numChoice second-row">
      <div class="col-md-5ths">
      <div class="num">1</div>
      </div>
      <div class="col-md-5ths">
      <div class="num">2</div>
      </div>
      <div class="col-md-5ths">
      <div class="num">3</div>
      </div>
      <div class="col-md-5ths">
      <div class="num">4</div>
      </div>
      <div class="col-md-5ths">
      <div class="num">5</div>
      </div>
      </div>
      </div>





      $('.numChoice > .col-md-5ths > .num').on("click", function() {
      var idx = $(this).parent().index();
      var act = $(this).is(".active");
      var $otherSide = $(this).closest(".numChoice").siblings(); // plural but only one
      var $otherElement = $otherSide.find(".col-md-5ths").eq(idx).children(":first");
      $(this).toggleClass('active');
      $otherElement.toggleClass('disabled', !act);

      var len1 = $otherSide.find('.active').length;
      var len2 = $(this).closest('.numChoice').find('.active').length;
      console.log('selection1: ' + len1, 'selection2: ' + len2);

      });

      .numChoice {
      display: block;
      }

      .num {
      width: 20px;
      height: 20px;
      background-color: #DDD;
      text-align: center;
      float: left;
      cursor: pointer;
      }

      .num.active {
      color: green;
      }

      .num.disabled {
      color: red;
      }

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <div class="container">
      <div class="numChoice first-row">
      <div class="col-md-5ths">
      <div class="num">1</div>
      </div>
      <div class="col-md-5ths">
      <div class="num">2</div>
      </div>
      <div class="col-md-5ths">
      <div class="num">3</div>
      </div>
      <div class="col-md-5ths">
      <div class="num">4</div>
      </div>
      <div class="col-md-5ths">
      <div class="num">5</div>
      </div>
      </div>
      <br><br>
      <div class="numChoice second-row">
      <div class="col-md-5ths">
      <div class="num">1</div>
      </div>
      <div class="col-md-5ths">
      <div class="num">2</div>
      </div>
      <div class="col-md-5ths">
      <div class="num">3</div>
      </div>
      <div class="col-md-5ths">
      <div class="num">4</div>
      </div>
      <div class="col-md-5ths">
      <div class="num">5</div>
      </div>
      </div>
      </div>






      javascript jquery






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 16 '18 at 4:40









      user123user123

      9214




      9214
























          2 Answers
          2






          active

          oldest

          votes


















          1














          If I understand, you want to count the active classes for the first row against the second row:



          const firstRowActiveLabels = $('.numChoice.first-row .active').length;
          const secondRowActiveLabels = $('.numChoice.second-row .active').length;


          .numChoice.first-row means: get the class that has numChoice and has first-row classes.
          .active means: and get the inner elements that has active class.






          $('.numChoice > .col-md-5ths > .num').on("click", function() {
          var idx = $(this).parent().index();
          var act = $(this).is(".active");
          var $otherSide = $(this).closest(".numChoice").siblings(); // plural but only one
          var $otherElement = $otherSide.find(".col-md-5ths").eq(idx).children(":first");
          $(this).toggleClass('active');
          $otherElement.toggleClass('disabled', !act);

          var len1 = $('.numChoice.first-row .active').length;
          var len2 = $('.numChoice.second-row .active').length;
          console.log('selection1: ' + len1, 'selection2: ' + len2);

          });

          .numChoice {
          display: block;
          }

          .num {
          width: 20px;
          height: 20px;
          background-color: #DDD;
          text-align: center;
          float: left;
          cursor: pointer;
          }

          .num.active {
          color: green;
          }

          .num.disabled {
          color: red;
          }

          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
          <div class="container">
          <div class="numChoice first-row">
          <div class="col-md-5ths">
          <div class="num">1</div>
          </div>
          <div class="col-md-5ths">
          <div class="num">2</div>
          </div>
          <div class="col-md-5ths">
          <div class="num">3</div>
          </div>
          <div class="col-md-5ths">
          <div class="num">4</div>
          </div>
          <div class="col-md-5ths">
          <div class="num">5</div>
          </div>
          </div>
          <br><br>
          <div class="numChoice second-row">
          <div class="col-md-5ths">
          <div class="num">1</div>
          </div>
          <div class="col-md-5ths">
          <div class="num">2</div>
          </div>
          <div class="col-md-5ths">
          <div class="num">3</div>
          </div>
          <div class="col-md-5ths">
          <div class="num">4</div>
          </div>
          <div class="col-md-5ths">
          <div class="num">5</div>
          </div>
          </div>
          </div>








          share|improve this answer































            1














            I've rewritten your JS part a bit. Please check if it's what you're looking for.






            $('.numChoice .num').on("click", function() {
            if ($(this).hasClass('disabled')){
            return false; // let's skip disabled items?
            }
            var idx = $(this).parent().index();
            $(this).toggleClass('active');
            var act = $(this).is(".active");
            const $others = $(this).closest(".numChoice").siblings();
            $others.each(function(){
            const $opposite = $(this).find('.num').eq(idx);
            $opposite.removeClass('active').toggleClass('disabled', act);
            });
            updateCounts();

            });

            function updateCounts(){
            const len1 = $('.first-row .active').length;
            const len2 = $('.second-row .active').length;
            console.clear();
            console.log('selection1: ' + len1, 'selection2: ' + len2);
            }

            .numChoice {
            display: block;
            }

            .num {
            width: 20px;
            height: 20px;
            background-color: #DDD;
            text-align: center;
            float: left;
            cursor: pointer;
            }

            .num.active {
            color: green;
            }

            .num.disabled {
            color: red;
            }

            <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
            <div class="container">
            <div class="numChoice first-row">
            <div class="col-md-5ths">
            <div class="num">1</div>
            </div>
            <div class="col-md-5ths">
            <div class="num">2</div>
            </div>
            <div class="col-md-5ths">
            <div class="num">3</div>
            </div>
            <div class="col-md-5ths">
            <div class="num">4</div>
            </div>
            <div class="col-md-5ths">
            <div class="num">5</div>
            </div>
            </div>
            <br><br>
            <div class="numChoice second-row">
            <div class="col-md-5ths">
            <div class="num">1</div>
            </div>
            <div class="col-md-5ths">
            <div class="num">2</div>
            </div>
            <div class="col-md-5ths">
            <div class="num">3</div>
            </div>
            <div class="col-md-5ths">
            <div class="num">4</div>
            </div>
            <div class="col-md-5ths">
            <div class="num">5</div>
            </div>
            </div>
            </div>








            share|improve this answer
























            • ohhh.. I thought my answer was correct ) okay

              – Roman86
              Nov 16 '18 at 5:53











            • you're answer is correct, I can't two accept two answers

              – user123
              Nov 16 '18 at 6:03











            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53331534%2fcount-the-active-classes-on-both-choices%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            If I understand, you want to count the active classes for the first row against the second row:



            const firstRowActiveLabels = $('.numChoice.first-row .active').length;
            const secondRowActiveLabels = $('.numChoice.second-row .active').length;


            .numChoice.first-row means: get the class that has numChoice and has first-row classes.
            .active means: and get the inner elements that has active class.






            $('.numChoice > .col-md-5ths > .num').on("click", function() {
            var idx = $(this).parent().index();
            var act = $(this).is(".active");
            var $otherSide = $(this).closest(".numChoice").siblings(); // plural but only one
            var $otherElement = $otherSide.find(".col-md-5ths").eq(idx).children(":first");
            $(this).toggleClass('active');
            $otherElement.toggleClass('disabled', !act);

            var len1 = $('.numChoice.first-row .active').length;
            var len2 = $('.numChoice.second-row .active').length;
            console.log('selection1: ' + len1, 'selection2: ' + len2);

            });

            .numChoice {
            display: block;
            }

            .num {
            width: 20px;
            height: 20px;
            background-color: #DDD;
            text-align: center;
            float: left;
            cursor: pointer;
            }

            .num.active {
            color: green;
            }

            .num.disabled {
            color: red;
            }

            <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
            <div class="container">
            <div class="numChoice first-row">
            <div class="col-md-5ths">
            <div class="num">1</div>
            </div>
            <div class="col-md-5ths">
            <div class="num">2</div>
            </div>
            <div class="col-md-5ths">
            <div class="num">3</div>
            </div>
            <div class="col-md-5ths">
            <div class="num">4</div>
            </div>
            <div class="col-md-5ths">
            <div class="num">5</div>
            </div>
            </div>
            <br><br>
            <div class="numChoice second-row">
            <div class="col-md-5ths">
            <div class="num">1</div>
            </div>
            <div class="col-md-5ths">
            <div class="num">2</div>
            </div>
            <div class="col-md-5ths">
            <div class="num">3</div>
            </div>
            <div class="col-md-5ths">
            <div class="num">4</div>
            </div>
            <div class="col-md-5ths">
            <div class="num">5</div>
            </div>
            </div>
            </div>








            share|improve this answer




























              1














              If I understand, you want to count the active classes for the first row against the second row:



              const firstRowActiveLabels = $('.numChoice.first-row .active').length;
              const secondRowActiveLabels = $('.numChoice.second-row .active').length;


              .numChoice.first-row means: get the class that has numChoice and has first-row classes.
              .active means: and get the inner elements that has active class.






              $('.numChoice > .col-md-5ths > .num').on("click", function() {
              var idx = $(this).parent().index();
              var act = $(this).is(".active");
              var $otherSide = $(this).closest(".numChoice").siblings(); // plural but only one
              var $otherElement = $otherSide.find(".col-md-5ths").eq(idx).children(":first");
              $(this).toggleClass('active');
              $otherElement.toggleClass('disabled', !act);

              var len1 = $('.numChoice.first-row .active').length;
              var len2 = $('.numChoice.second-row .active').length;
              console.log('selection1: ' + len1, 'selection2: ' + len2);

              });

              .numChoice {
              display: block;
              }

              .num {
              width: 20px;
              height: 20px;
              background-color: #DDD;
              text-align: center;
              float: left;
              cursor: pointer;
              }

              .num.active {
              color: green;
              }

              .num.disabled {
              color: red;
              }

              <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
              <div class="container">
              <div class="numChoice first-row">
              <div class="col-md-5ths">
              <div class="num">1</div>
              </div>
              <div class="col-md-5ths">
              <div class="num">2</div>
              </div>
              <div class="col-md-5ths">
              <div class="num">3</div>
              </div>
              <div class="col-md-5ths">
              <div class="num">4</div>
              </div>
              <div class="col-md-5ths">
              <div class="num">5</div>
              </div>
              </div>
              <br><br>
              <div class="numChoice second-row">
              <div class="col-md-5ths">
              <div class="num">1</div>
              </div>
              <div class="col-md-5ths">
              <div class="num">2</div>
              </div>
              <div class="col-md-5ths">
              <div class="num">3</div>
              </div>
              <div class="col-md-5ths">
              <div class="num">4</div>
              </div>
              <div class="col-md-5ths">
              <div class="num">5</div>
              </div>
              </div>
              </div>








              share|improve this answer


























                1












                1








                1







                If I understand, you want to count the active classes for the first row against the second row:



                const firstRowActiveLabels = $('.numChoice.first-row .active').length;
                const secondRowActiveLabels = $('.numChoice.second-row .active').length;


                .numChoice.first-row means: get the class that has numChoice and has first-row classes.
                .active means: and get the inner elements that has active class.






                $('.numChoice > .col-md-5ths > .num').on("click", function() {
                var idx = $(this).parent().index();
                var act = $(this).is(".active");
                var $otherSide = $(this).closest(".numChoice").siblings(); // plural but only one
                var $otherElement = $otherSide.find(".col-md-5ths").eq(idx).children(":first");
                $(this).toggleClass('active');
                $otherElement.toggleClass('disabled', !act);

                var len1 = $('.numChoice.first-row .active').length;
                var len2 = $('.numChoice.second-row .active').length;
                console.log('selection1: ' + len1, 'selection2: ' + len2);

                });

                .numChoice {
                display: block;
                }

                .num {
                width: 20px;
                height: 20px;
                background-color: #DDD;
                text-align: center;
                float: left;
                cursor: pointer;
                }

                .num.active {
                color: green;
                }

                .num.disabled {
                color: red;
                }

                <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                <div class="container">
                <div class="numChoice first-row">
                <div class="col-md-5ths">
                <div class="num">1</div>
                </div>
                <div class="col-md-5ths">
                <div class="num">2</div>
                </div>
                <div class="col-md-5ths">
                <div class="num">3</div>
                </div>
                <div class="col-md-5ths">
                <div class="num">4</div>
                </div>
                <div class="col-md-5ths">
                <div class="num">5</div>
                </div>
                </div>
                <br><br>
                <div class="numChoice second-row">
                <div class="col-md-5ths">
                <div class="num">1</div>
                </div>
                <div class="col-md-5ths">
                <div class="num">2</div>
                </div>
                <div class="col-md-5ths">
                <div class="num">3</div>
                </div>
                <div class="col-md-5ths">
                <div class="num">4</div>
                </div>
                <div class="col-md-5ths">
                <div class="num">5</div>
                </div>
                </div>
                </div>








                share|improve this answer













                If I understand, you want to count the active classes for the first row against the second row:



                const firstRowActiveLabels = $('.numChoice.first-row .active').length;
                const secondRowActiveLabels = $('.numChoice.second-row .active').length;


                .numChoice.first-row means: get the class that has numChoice and has first-row classes.
                .active means: and get the inner elements that has active class.






                $('.numChoice > .col-md-5ths > .num').on("click", function() {
                var idx = $(this).parent().index();
                var act = $(this).is(".active");
                var $otherSide = $(this).closest(".numChoice").siblings(); // plural but only one
                var $otherElement = $otherSide.find(".col-md-5ths").eq(idx).children(":first");
                $(this).toggleClass('active');
                $otherElement.toggleClass('disabled', !act);

                var len1 = $('.numChoice.first-row .active').length;
                var len2 = $('.numChoice.second-row .active').length;
                console.log('selection1: ' + len1, 'selection2: ' + len2);

                });

                .numChoice {
                display: block;
                }

                .num {
                width: 20px;
                height: 20px;
                background-color: #DDD;
                text-align: center;
                float: left;
                cursor: pointer;
                }

                .num.active {
                color: green;
                }

                .num.disabled {
                color: red;
                }

                <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                <div class="container">
                <div class="numChoice first-row">
                <div class="col-md-5ths">
                <div class="num">1</div>
                </div>
                <div class="col-md-5ths">
                <div class="num">2</div>
                </div>
                <div class="col-md-5ths">
                <div class="num">3</div>
                </div>
                <div class="col-md-5ths">
                <div class="num">4</div>
                </div>
                <div class="col-md-5ths">
                <div class="num">5</div>
                </div>
                </div>
                <br><br>
                <div class="numChoice second-row">
                <div class="col-md-5ths">
                <div class="num">1</div>
                </div>
                <div class="col-md-5ths">
                <div class="num">2</div>
                </div>
                <div class="col-md-5ths">
                <div class="num">3</div>
                </div>
                <div class="col-md-5ths">
                <div class="num">4</div>
                </div>
                <div class="col-md-5ths">
                <div class="num">5</div>
                </div>
                </div>
                </div>








                $('.numChoice > .col-md-5ths > .num').on("click", function() {
                var idx = $(this).parent().index();
                var act = $(this).is(".active");
                var $otherSide = $(this).closest(".numChoice").siblings(); // plural but only one
                var $otherElement = $otherSide.find(".col-md-5ths").eq(idx).children(":first");
                $(this).toggleClass('active');
                $otherElement.toggleClass('disabled', !act);

                var len1 = $('.numChoice.first-row .active').length;
                var len2 = $('.numChoice.second-row .active').length;
                console.log('selection1: ' + len1, 'selection2: ' + len2);

                });

                .numChoice {
                display: block;
                }

                .num {
                width: 20px;
                height: 20px;
                background-color: #DDD;
                text-align: center;
                float: left;
                cursor: pointer;
                }

                .num.active {
                color: green;
                }

                .num.disabled {
                color: red;
                }

                <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                <div class="container">
                <div class="numChoice first-row">
                <div class="col-md-5ths">
                <div class="num">1</div>
                </div>
                <div class="col-md-5ths">
                <div class="num">2</div>
                </div>
                <div class="col-md-5ths">
                <div class="num">3</div>
                </div>
                <div class="col-md-5ths">
                <div class="num">4</div>
                </div>
                <div class="col-md-5ths">
                <div class="num">5</div>
                </div>
                </div>
                <br><br>
                <div class="numChoice second-row">
                <div class="col-md-5ths">
                <div class="num">1</div>
                </div>
                <div class="col-md-5ths">
                <div class="num">2</div>
                </div>
                <div class="col-md-5ths">
                <div class="num">3</div>
                </div>
                <div class="col-md-5ths">
                <div class="num">4</div>
                </div>
                <div class="col-md-5ths">
                <div class="num">5</div>
                </div>
                </div>
                </div>





                $('.numChoice > .col-md-5ths > .num').on("click", function() {
                var idx = $(this).parent().index();
                var act = $(this).is(".active");
                var $otherSide = $(this).closest(".numChoice").siblings(); // plural but only one
                var $otherElement = $otherSide.find(".col-md-5ths").eq(idx).children(":first");
                $(this).toggleClass('active');
                $otherElement.toggleClass('disabled', !act);

                var len1 = $('.numChoice.first-row .active').length;
                var len2 = $('.numChoice.second-row .active').length;
                console.log('selection1: ' + len1, 'selection2: ' + len2);

                });

                .numChoice {
                display: block;
                }

                .num {
                width: 20px;
                height: 20px;
                background-color: #DDD;
                text-align: center;
                float: left;
                cursor: pointer;
                }

                .num.active {
                color: green;
                }

                .num.disabled {
                color: red;
                }

                <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                <div class="container">
                <div class="numChoice first-row">
                <div class="col-md-5ths">
                <div class="num">1</div>
                </div>
                <div class="col-md-5ths">
                <div class="num">2</div>
                </div>
                <div class="col-md-5ths">
                <div class="num">3</div>
                </div>
                <div class="col-md-5ths">
                <div class="num">4</div>
                </div>
                <div class="col-md-5ths">
                <div class="num">5</div>
                </div>
                </div>
                <br><br>
                <div class="numChoice second-row">
                <div class="col-md-5ths">
                <div class="num">1</div>
                </div>
                <div class="col-md-5ths">
                <div class="num">2</div>
                </div>
                <div class="col-md-5ths">
                <div class="num">3</div>
                </div>
                <div class="col-md-5ths">
                <div class="num">4</div>
                </div>
                <div class="col-md-5ths">
                <div class="num">5</div>
                </div>
                </div>
                </div>






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 16 '18 at 4:48









                Alon ShmielAlon Shmiel

                1,1861667113




                1,1861667113

























                    1














                    I've rewritten your JS part a bit. Please check if it's what you're looking for.






                    $('.numChoice .num').on("click", function() {
                    if ($(this).hasClass('disabled')){
                    return false; // let's skip disabled items?
                    }
                    var idx = $(this).parent().index();
                    $(this).toggleClass('active');
                    var act = $(this).is(".active");
                    const $others = $(this).closest(".numChoice").siblings();
                    $others.each(function(){
                    const $opposite = $(this).find('.num').eq(idx);
                    $opposite.removeClass('active').toggleClass('disabled', act);
                    });
                    updateCounts();

                    });

                    function updateCounts(){
                    const len1 = $('.first-row .active').length;
                    const len2 = $('.second-row .active').length;
                    console.clear();
                    console.log('selection1: ' + len1, 'selection2: ' + len2);
                    }

                    .numChoice {
                    display: block;
                    }

                    .num {
                    width: 20px;
                    height: 20px;
                    background-color: #DDD;
                    text-align: center;
                    float: left;
                    cursor: pointer;
                    }

                    .num.active {
                    color: green;
                    }

                    .num.disabled {
                    color: red;
                    }

                    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                    <div class="container">
                    <div class="numChoice first-row">
                    <div class="col-md-5ths">
                    <div class="num">1</div>
                    </div>
                    <div class="col-md-5ths">
                    <div class="num">2</div>
                    </div>
                    <div class="col-md-5ths">
                    <div class="num">3</div>
                    </div>
                    <div class="col-md-5ths">
                    <div class="num">4</div>
                    </div>
                    <div class="col-md-5ths">
                    <div class="num">5</div>
                    </div>
                    </div>
                    <br><br>
                    <div class="numChoice second-row">
                    <div class="col-md-5ths">
                    <div class="num">1</div>
                    </div>
                    <div class="col-md-5ths">
                    <div class="num">2</div>
                    </div>
                    <div class="col-md-5ths">
                    <div class="num">3</div>
                    </div>
                    <div class="col-md-5ths">
                    <div class="num">4</div>
                    </div>
                    <div class="col-md-5ths">
                    <div class="num">5</div>
                    </div>
                    </div>
                    </div>








                    share|improve this answer
























                    • ohhh.. I thought my answer was correct ) okay

                      – Roman86
                      Nov 16 '18 at 5:53











                    • you're answer is correct, I can't two accept two answers

                      – user123
                      Nov 16 '18 at 6:03
















                    1














                    I've rewritten your JS part a bit. Please check if it's what you're looking for.






                    $('.numChoice .num').on("click", function() {
                    if ($(this).hasClass('disabled')){
                    return false; // let's skip disabled items?
                    }
                    var idx = $(this).parent().index();
                    $(this).toggleClass('active');
                    var act = $(this).is(".active");
                    const $others = $(this).closest(".numChoice").siblings();
                    $others.each(function(){
                    const $opposite = $(this).find('.num').eq(idx);
                    $opposite.removeClass('active').toggleClass('disabled', act);
                    });
                    updateCounts();

                    });

                    function updateCounts(){
                    const len1 = $('.first-row .active').length;
                    const len2 = $('.second-row .active').length;
                    console.clear();
                    console.log('selection1: ' + len1, 'selection2: ' + len2);
                    }

                    .numChoice {
                    display: block;
                    }

                    .num {
                    width: 20px;
                    height: 20px;
                    background-color: #DDD;
                    text-align: center;
                    float: left;
                    cursor: pointer;
                    }

                    .num.active {
                    color: green;
                    }

                    .num.disabled {
                    color: red;
                    }

                    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                    <div class="container">
                    <div class="numChoice first-row">
                    <div class="col-md-5ths">
                    <div class="num">1</div>
                    </div>
                    <div class="col-md-5ths">
                    <div class="num">2</div>
                    </div>
                    <div class="col-md-5ths">
                    <div class="num">3</div>
                    </div>
                    <div class="col-md-5ths">
                    <div class="num">4</div>
                    </div>
                    <div class="col-md-5ths">
                    <div class="num">5</div>
                    </div>
                    </div>
                    <br><br>
                    <div class="numChoice second-row">
                    <div class="col-md-5ths">
                    <div class="num">1</div>
                    </div>
                    <div class="col-md-5ths">
                    <div class="num">2</div>
                    </div>
                    <div class="col-md-5ths">
                    <div class="num">3</div>
                    </div>
                    <div class="col-md-5ths">
                    <div class="num">4</div>
                    </div>
                    <div class="col-md-5ths">
                    <div class="num">5</div>
                    </div>
                    </div>
                    </div>








                    share|improve this answer
























                    • ohhh.. I thought my answer was correct ) okay

                      – Roman86
                      Nov 16 '18 at 5:53











                    • you're answer is correct, I can't two accept two answers

                      – user123
                      Nov 16 '18 at 6:03














                    1












                    1








                    1







                    I've rewritten your JS part a bit. Please check if it's what you're looking for.






                    $('.numChoice .num').on("click", function() {
                    if ($(this).hasClass('disabled')){
                    return false; // let's skip disabled items?
                    }
                    var idx = $(this).parent().index();
                    $(this).toggleClass('active');
                    var act = $(this).is(".active");
                    const $others = $(this).closest(".numChoice").siblings();
                    $others.each(function(){
                    const $opposite = $(this).find('.num').eq(idx);
                    $opposite.removeClass('active').toggleClass('disabled', act);
                    });
                    updateCounts();

                    });

                    function updateCounts(){
                    const len1 = $('.first-row .active').length;
                    const len2 = $('.second-row .active').length;
                    console.clear();
                    console.log('selection1: ' + len1, 'selection2: ' + len2);
                    }

                    .numChoice {
                    display: block;
                    }

                    .num {
                    width: 20px;
                    height: 20px;
                    background-color: #DDD;
                    text-align: center;
                    float: left;
                    cursor: pointer;
                    }

                    .num.active {
                    color: green;
                    }

                    .num.disabled {
                    color: red;
                    }

                    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                    <div class="container">
                    <div class="numChoice first-row">
                    <div class="col-md-5ths">
                    <div class="num">1</div>
                    </div>
                    <div class="col-md-5ths">
                    <div class="num">2</div>
                    </div>
                    <div class="col-md-5ths">
                    <div class="num">3</div>
                    </div>
                    <div class="col-md-5ths">
                    <div class="num">4</div>
                    </div>
                    <div class="col-md-5ths">
                    <div class="num">5</div>
                    </div>
                    </div>
                    <br><br>
                    <div class="numChoice second-row">
                    <div class="col-md-5ths">
                    <div class="num">1</div>
                    </div>
                    <div class="col-md-5ths">
                    <div class="num">2</div>
                    </div>
                    <div class="col-md-5ths">
                    <div class="num">3</div>
                    </div>
                    <div class="col-md-5ths">
                    <div class="num">4</div>
                    </div>
                    <div class="col-md-5ths">
                    <div class="num">5</div>
                    </div>
                    </div>
                    </div>








                    share|improve this answer













                    I've rewritten your JS part a bit. Please check if it's what you're looking for.






                    $('.numChoice .num').on("click", function() {
                    if ($(this).hasClass('disabled')){
                    return false; // let's skip disabled items?
                    }
                    var idx = $(this).parent().index();
                    $(this).toggleClass('active');
                    var act = $(this).is(".active");
                    const $others = $(this).closest(".numChoice").siblings();
                    $others.each(function(){
                    const $opposite = $(this).find('.num').eq(idx);
                    $opposite.removeClass('active').toggleClass('disabled', act);
                    });
                    updateCounts();

                    });

                    function updateCounts(){
                    const len1 = $('.first-row .active').length;
                    const len2 = $('.second-row .active').length;
                    console.clear();
                    console.log('selection1: ' + len1, 'selection2: ' + len2);
                    }

                    .numChoice {
                    display: block;
                    }

                    .num {
                    width: 20px;
                    height: 20px;
                    background-color: #DDD;
                    text-align: center;
                    float: left;
                    cursor: pointer;
                    }

                    .num.active {
                    color: green;
                    }

                    .num.disabled {
                    color: red;
                    }

                    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                    <div class="container">
                    <div class="numChoice first-row">
                    <div class="col-md-5ths">
                    <div class="num">1</div>
                    </div>
                    <div class="col-md-5ths">
                    <div class="num">2</div>
                    </div>
                    <div class="col-md-5ths">
                    <div class="num">3</div>
                    </div>
                    <div class="col-md-5ths">
                    <div class="num">4</div>
                    </div>
                    <div class="col-md-5ths">
                    <div class="num">5</div>
                    </div>
                    </div>
                    <br><br>
                    <div class="numChoice second-row">
                    <div class="col-md-5ths">
                    <div class="num">1</div>
                    </div>
                    <div class="col-md-5ths">
                    <div class="num">2</div>
                    </div>
                    <div class="col-md-5ths">
                    <div class="num">3</div>
                    </div>
                    <div class="col-md-5ths">
                    <div class="num">4</div>
                    </div>
                    <div class="col-md-5ths">
                    <div class="num">5</div>
                    </div>
                    </div>
                    </div>








                    $('.numChoice .num').on("click", function() {
                    if ($(this).hasClass('disabled')){
                    return false; // let's skip disabled items?
                    }
                    var idx = $(this).parent().index();
                    $(this).toggleClass('active');
                    var act = $(this).is(".active");
                    const $others = $(this).closest(".numChoice").siblings();
                    $others.each(function(){
                    const $opposite = $(this).find('.num').eq(idx);
                    $opposite.removeClass('active').toggleClass('disabled', act);
                    });
                    updateCounts();

                    });

                    function updateCounts(){
                    const len1 = $('.first-row .active').length;
                    const len2 = $('.second-row .active').length;
                    console.clear();
                    console.log('selection1: ' + len1, 'selection2: ' + len2);
                    }

                    .numChoice {
                    display: block;
                    }

                    .num {
                    width: 20px;
                    height: 20px;
                    background-color: #DDD;
                    text-align: center;
                    float: left;
                    cursor: pointer;
                    }

                    .num.active {
                    color: green;
                    }

                    .num.disabled {
                    color: red;
                    }

                    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                    <div class="container">
                    <div class="numChoice first-row">
                    <div class="col-md-5ths">
                    <div class="num">1</div>
                    </div>
                    <div class="col-md-5ths">
                    <div class="num">2</div>
                    </div>
                    <div class="col-md-5ths">
                    <div class="num">3</div>
                    </div>
                    <div class="col-md-5ths">
                    <div class="num">4</div>
                    </div>
                    <div class="col-md-5ths">
                    <div class="num">5</div>
                    </div>
                    </div>
                    <br><br>
                    <div class="numChoice second-row">
                    <div class="col-md-5ths">
                    <div class="num">1</div>
                    </div>
                    <div class="col-md-5ths">
                    <div class="num">2</div>
                    </div>
                    <div class="col-md-5ths">
                    <div class="num">3</div>
                    </div>
                    <div class="col-md-5ths">
                    <div class="num">4</div>
                    </div>
                    <div class="col-md-5ths">
                    <div class="num">5</div>
                    </div>
                    </div>
                    </div>





                    $('.numChoice .num').on("click", function() {
                    if ($(this).hasClass('disabled')){
                    return false; // let's skip disabled items?
                    }
                    var idx = $(this).parent().index();
                    $(this).toggleClass('active');
                    var act = $(this).is(".active");
                    const $others = $(this).closest(".numChoice").siblings();
                    $others.each(function(){
                    const $opposite = $(this).find('.num').eq(idx);
                    $opposite.removeClass('active').toggleClass('disabled', act);
                    });
                    updateCounts();

                    });

                    function updateCounts(){
                    const len1 = $('.first-row .active').length;
                    const len2 = $('.second-row .active').length;
                    console.clear();
                    console.log('selection1: ' + len1, 'selection2: ' + len2);
                    }

                    .numChoice {
                    display: block;
                    }

                    .num {
                    width: 20px;
                    height: 20px;
                    background-color: #DDD;
                    text-align: center;
                    float: left;
                    cursor: pointer;
                    }

                    .num.active {
                    color: green;
                    }

                    .num.disabled {
                    color: red;
                    }

                    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                    <div class="container">
                    <div class="numChoice first-row">
                    <div class="col-md-5ths">
                    <div class="num">1</div>
                    </div>
                    <div class="col-md-5ths">
                    <div class="num">2</div>
                    </div>
                    <div class="col-md-5ths">
                    <div class="num">3</div>
                    </div>
                    <div class="col-md-5ths">
                    <div class="num">4</div>
                    </div>
                    <div class="col-md-5ths">
                    <div class="num">5</div>
                    </div>
                    </div>
                    <br><br>
                    <div class="numChoice second-row">
                    <div class="col-md-5ths">
                    <div class="num">1</div>
                    </div>
                    <div class="col-md-5ths">
                    <div class="num">2</div>
                    </div>
                    <div class="col-md-5ths">
                    <div class="num">3</div>
                    </div>
                    <div class="col-md-5ths">
                    <div class="num">4</div>
                    </div>
                    <div class="col-md-5ths">
                    <div class="num">5</div>
                    </div>
                    </div>
                    </div>






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 16 '18 at 4:54









                    Roman86Roman86

                    846610




                    846610













                    • ohhh.. I thought my answer was correct ) okay

                      – Roman86
                      Nov 16 '18 at 5:53











                    • you're answer is correct, I can't two accept two answers

                      – user123
                      Nov 16 '18 at 6:03



















                    • ohhh.. I thought my answer was correct ) okay

                      – Roman86
                      Nov 16 '18 at 5:53











                    • you're answer is correct, I can't two accept two answers

                      – user123
                      Nov 16 '18 at 6:03

















                    ohhh.. I thought my answer was correct ) okay

                    – Roman86
                    Nov 16 '18 at 5:53





                    ohhh.. I thought my answer was correct ) okay

                    – Roman86
                    Nov 16 '18 at 5:53













                    you're answer is correct, I can't two accept two answers

                    – user123
                    Nov 16 '18 at 6:03





                    you're answer is correct, I can't two accept two answers

                    – user123
                    Nov 16 '18 at 6:03


















                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53331534%2fcount-the-active-classes-on-both-choices%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    List item for chat from Array inside array React Native

                    Thiostrepton

                    Caerphilly