JavaScript conditionial toggler button

My code is for FAQ page:

function initActions() {
  let counter = 0;

  // Function to update button label based on counter
  const updateButtonLabel = () => {
    const toggleButton = document.querySelector('.js_faq_toggle_all');
    if (counter === 0 || counter < 4) {
      toggleButton.textContent = 'Show All';
      toggleButton.removeEventListener('click', toggleHideAll);
      toggleButton.addEventListener('click', toggleShowAll);
    } else {
      toggleButton.textContent = 'Hide All';
      toggleButton.removeEventListener('click', toggleShowAll);
      toggleButton.addEventListener('click', toggleHideAll);
    }
  };

  // Function to count visible items
  const updateCounter = () => {
    counter = document.querySelectorAll('.accordion-collapse.show').length;
    updateButtonLabel();
  };

  // Function to show all items
  const toggleShowAll = () => {
    const collapseElements = document.querySelectorAll('#faq .collapse');
    collapseElements.forEach(collapseEl => {
      const collapse = new bootstrap.Collapse(collapseEl, {toggle: false});
      collapse.show();
    });
    counter = collapseElements.length;
    updateButtonLabel();
  };

  // Function to hide all items
const toggleHideAll = () => {
  const collapseElements = document.querySelectorAll('#faq .collapse');
  collapseElements.forEach(collapseEl => {
    const collapse = new bootstrap.Collapse(collapseEl, {toggle: false});
    collapse.hide();
    collapseEl.removeEventListener('shown.bs.collapse', updateCounter);
    collapseEl.removeEventListener('hidden.bs.collapse', updateCounter);
  });
  counter = 0;
  updateButtonLabel();
};
  // Add event listeners to accordion buttons
  document.querySelectorAll('.accordion-button').forEach(button => {
    button.addEventListener('click', () => {
      const target = document.querySelector(button.getAttribute('data-bs-target'));
      target.addEventListener('shown.bs.collapse', updateCounter);
      target.addEventListener('hidden.bs.collapse', updateCounter);
    });
  });

  // Initial button setup
  const toggleButton = document.querySelector('.js_faq_toggle_all');
  toggleButton.addEventListener('click', toggleShowAll);
  updateButtonLabel();

  // Add event listener to search button
const searchButton = document.getElementById('searchButton');
const cancelSearchButton = document.getElementById('cancelSearchButton');

searchButton.addEventListener('click', () => {
  const searchText = document.getElementById('searchInput').value.toLowerCase();
  const items = document.querySelectorAll('.accordion-item');
  items.forEach(item => {
    const question = item.querySelector('.accordion-button').textContent.toLowerCase();
    const answer = item.querySelector('.accordion-body').textContent.toLowerCase();
    if (question.includes(searchText) || answer.includes(searchText)) {
      item.style.display = '';
    } else {
      item.style.display = 'none';
    }
  });
  cancelSearchButton.style.display = 'inline-block';
});

// Add event listener to cancel search button
cancelSearchButton.addEventListener('click', () => {
  const items = document.querySelectorAll('.accordion-item');
  items.forEach(item => {
    item.style.display = '';
  });
  document.getElementById('searchInput').value = '';
  cancelSearchButton.style.display = 'none';
});

// Add hover effect to cancel search button
cancelSearchButton.addEventListener('mouseover', () => {
  cancelSearchButton.style.backgroundColor = 'darkred';
});

cancelSearchButton.addEventListener('mouseout', () => {
  cancelSearchButton.style.backgroundColor = 'red';
});
}

// Use DOMContentLoaded event for better performance
document.addEventListener("DOMContentLoaded", initActions);

The problem is when I search for texts within button, then the 1st toggle change is bugged(causing wrongly added function and name for hide or show), but this case will be easy to solve.

But the real problem is, how can I write this code a bit better + how to add another condition and that’s language settings of client, so button name will have correct language translation?

I am a begginer in JS, so I have to ask. 😀 :/

Correct toggle button name changes + correctly attached function + adding language translation.

New contributor

PBT is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật