NazariyM
10/30/2018 - 1:38 PM

Checbkox Group


class ChecbkoxGroup {
  constructor(el) {
    this.block = (el);
    this.checkBoxes = this.block.querySelectorAll('.js-checkbox');
    this.checkBoxAll = this.block.querySelector('.js-checkbox-all');

    if (!this.block) return;

    this.init();
  }

  init() {
    this.checkAll();
    this.checkBoxAll.addEventListener('change', this.checkAll.bind(this));
  }

  checkAll() {
    if (this.checkBoxAll.checked === true) for (const checkBox of this.checkBoxes) checkBox.checked = true;
    if (this.checkBoxAll.checked === false) for (const checkBox of this.checkBoxes) checkBox.checked = false;
  }
}

const checkBoxGroups = document.querySelectorAll('.checkbox-group');
for (const group of checkBoxGroups) new ChecbkoxGroup(group);