How to make accordion doesn't work and tell me Cannot read property 'classList' of undefined if element existed?

This is what I have at the moment. This code doesn’t work as it should.

I need to make that – if I press for header element section must disappear, when I press again it must appear. I don’t understand why it tells me that element undefined if it existed in HTML…. Can anyone help me with this problem please?

var filterTrigger = document.querySelectorAll('.catalog__filter-form h3');
var filterSection = document.querySelectorAll('.catalog__filter-section');
var filterInputs = document.querySelectorAll('.catalog__filter-inputs');

for (var j = 0; j < filterTrigger.length; j++) {
  filterInputs[j].classList.add('filter-hidden');
  filterTrigger[j].addEventListener('click', function(evt) {
    evt.preventDefault();
    filterSection[j].classList.toggle('catalog__filter-section--opened');
    filterInputs[j].classList.toggle('filter-visible');
  });
}
.catalog__container {
  max-width: 1366px;
  margin: 0 auto;
}

.catalog__content-container {
  margin-left: 7.2%;
  margin-right: 7.2%;
  margin-top: 91px;
}

.catalog__filter-form {
  border: 1px solid brown;
  width: 248px;
}

.catalog__filter-form h3 {
  font-family: arial;
  font-size: 16px;
  font-weight: 500;
  line-height: 21px;
  color: black;
  margin: 0;
  padding-top: 22px;
  padding-bottom: 23px;
  padding-left: 24px;
}

.catalog__filter-section {
  padding-left: 24px;
  border-bottom: 1px solid brown;
  position: relative;
}

.catalog__filter-section::after {
  content: "";
  position: absolute;
  right: 22px;
  top: -38px;
  background-image: url("../img/arrow-down-icon.svg");
  background-repeat: no-repeat;
  width: 18px;
  height: 12px;
}

.catalog__filter-section--opened::after {
  background-image: url("../img/arrow-up-icon.svg")
}

.catalog__filter-section:last-child {
  border: none;
}

.catalog__filter-inputs {
  display: flex;
  flex-direction: column;
  display: flex;
  padding-bottom: 13px;
}

.catalog__filter-section label {
  font-family: arial;
  font-size: 14px;
  font-weight: 500;
  line-height: 18px;
  color: black;
  padding-left: 25px;
  position: relative;
  margin-bottom: 13px;
}

.catalog__filter-section input {
  appearance: none;
}

.catalog__filter-section input:checked+span {
  background-color: brown;
}

.catalog__filter-section span {
  position: absolute;
  width: 15px;
  height: 15px;
  left: 10px;
  top: 2px;
  border: 1px solid brown;
  background-color: white;
}

.filter-hidden {
  display: none;
}

.filter-visible {
  display: flex;
}
<form class="catalog__filter-form">
  <h3>Product</h3>
  <div class="catalog__filter-section catalog__filter-section--opened" tabindex="0">
    <div class="catalog__filter-inputs filter-visible">
      <label>
        <input type="checkbox" name="products" value="Necklaces" checked>
        <span></span>
        Necklaces
      </label>
      <label>
        <input type="checkbox" name="products" value="Chokers" checked>
        <span></span>
        Chokers
      </label>
      <label>
        <input type="checkbox" name="products" value="Rings">
        <span></span>
        Rings
        </label>
        <label>
          <input type="checkbox" name="products" value="Earrings" checked>
          <span></span>
          Earrings
          </label>
    </div>
  </div>
  <h3>Material</h3>
  <div class="catalog__filter-section" tabindex="0">
    <div class="catalog__filter-inputs">
      <label>
        <input type="checkbox" name="material" value="Gold">
        <span></span>
        Gold
      </label>
      <label>
        <input type="checkbox" name="material" value="Silver">
        <span></span>
        Silver
      </label>
    </div>
  </div>
  <h3>Collection</h3>
  <div class="catalog__filter-section" tabindex="0">
    <div class="catalog__filter-inputs">
      <label>
        <input type="checkbox" name="collection" value="Pink flamingo">
        <span></span>
        Pink flamingo
      </label>
      <label>
        <input type="checkbox" name="collection" value="Dreams">
        <span></span>
        Dreams
      </label>
    </div>
  </div>
  <h3>Price</h3>
  <div class="catalog__filter-section" tabindex="0">
    <div class="catalog__filter-inputs">
      <label>
        <input type="checkbox" name="products" value="Necklaces">
        Necklaces
      </label>
    </div>
  </div>
</form>
</div>

5 thoughts on “How to make accordion doesn't work and tell me Cannot read property 'classList' of undefined if element existed?”

  1. Thanks for finally talking about > How to make accordion doesn't work and
    tell me Cannot read property 'classList'
    of undefined if element existed? – JavaScript Help
    < Liked it!

    Reply
  2. Your style is really unique compared to other people I have read stuff from.
    Thanks for posting when you have the opportunity, Guess
    I will just bookmark this blog.

    Reply
  3. Интернет-платформа Джойказино
    появилась на просторах отечественного интернета более 7 лет назад.
    Посетителям веб-проекта предложен лучший софт от известных разработчиков.

    В подборку виртуальных развлечений входят слоты с барабанами и линиями,
    лайв-игры и ставки на спорт и киберспортивные дисциплины.

    Reply

Leave a Comment