Limited checkbox 2

by LuckyCat

HTML

<div class="checkbox-block">
  <div class="checkbox-item tr-option">
    Test text 1
  </div>
  <div class="checkbox-item tr-option">
    Test text 2
  </div>
  <div class="checkbox-item tr-option">
    Test text 3
  </div>
  <div class="checkbox-item tr-option">
    Test text 4
  </div>
  <div class="checkbox-item tr-option">
    Test text 5
  </div>
  <div class="checkbox-item tr-option">
    Test text 6
  </div>
</div>

<div style="height: 50px;"></div>

<div class="checkbox-block">
  <div class="checkbox-item tr-option">
    Test text 1
  </div>
  <div class="checkbox-item tr-option">
    Test text 2
  </div>
  <div class="checkbox-item tr-option">
    Test text 3
  </div>
  <div class="checkbox-item tr-option">
    Test text 4
  </div>
  <div class="checkbox-item tr-option">
    Test text 5
  </div>
  <div class="checkbox-item tr-option">
    Test text 6
  </div>
</div>

CSS

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

:focus {
  outline: 0;
}

img {
  border: none;
  display: block;
}

ol,
ul {
  list-style: none;
}

a {
  text-decoration: none;
  cursor: pointer !important;
}

html {
  font-size: 62.5%;
  -webkit-text-size-adjust: 100%;
  position: relative;
}

body {
  font-family: Arial, sans-serif;
  color: #333;
  line-height: 1.3;
  font-size: 1.6rem;
}

.checkbox-block {
  margin-top: 2rem;
}

.checkbox-item {
  position: relative;
  padding: 0 0 0 4.5rem;
  margin-bottom: 2rem;
  text-align: left;
  line-height: 3rem;
  cursor: pointer;
  transition: all 0.3s linear;
}

.checkbox-item::before {
  content: "";
  position: absolute;
  width: 3rem;
  height: 3rem;
  border: 1px solid #d3cfc8;
  top: 0;
  left: 0;
  line-height: 2.2rem;
  color: #fff;
  font-family: Arial, sans-serif;
  font-weight: bold;
  font-size: 1.6rem;
  text-align: center;
}

.checkbox-item.checked::before {
  background-color: #de9c7d;
}

.checkbox-item.checked::after {
  content: '';
  display: block;
  position: absolute;
  top: 0.1rem;
  left: 0.9rem;
  border: 0.6rem solid #fff;
  border-left: 0;
  border-top: 0;
  width: 0.8rem;
  height: 1.7rem;
  transform: rotate(45deg);
}

JavaScript

/* Checked options */
var selectedClassName = "checked";
var maxSelelectedItems = 3;
var customButtons = document.querySelectorAll('.checkbox-item');
[].forEach.call(customButtons, function(button) {
  button.addEventListener('click', function() {
    if (hasSelectedClass(this)) {
      removeSelectedClass(this);
    } else {
      if (countSelectedItems(this.parentElement) < maxSelelectedItems) {
        addSelectedClass(this);
      }
    }
  });
});

function addSelectedClass(item) {
  item.classList.add(selectedClassName);
}

function removeSelectedClass(item) {
  item.classList.remove(selectedClassName);
}

function hasSelectedClass(item) {
  return item.classList.contains(selectedClassName);
}

function countSelectedItems(checkboxBlock) {
  var selectedItems = checkboxBlock.querySelectorAll('.checkbox-item.checked');
  return selectedItems ? selectedItems.length : 0;
}