Javascript onclick event based on corresponding id iterations

I am attempting to create dynamic modal popups from query feeds in WordPress. One feed displays elements that need to be clicked and one feed holds the content for the modal popups. My current code adds an iterated id to all elements with a certain class if the parent has a certain id. This is occuring for the two different parent containers.

My problem is I want it to work so when you click on id=”team-0″ it will open the corresponding id=”modal-0″;

Here is my javascript:

const modalContainer = document.getElementById('modalContainer');
const modal = modalContainer.querySelectorAll('.team');

  modal.forEach((element, index) => {
    element.id = `modal-${index}`;
  });

const teamContainer = document.getElementById('teamContainer');
const team = teamContainer.querySelectorAll('.team');

  team.forEach((element, index) => {
    element.id = `team-${index}`;
  });

The HTML looks similar to this:

<div id="teamContainer">
<ul>
<li id="team-0" class="team"></li>
<li id="team-1" class="team"></li>
<li id="team-2" class="team"></li>
</ul>
</div>

<div id="modalContainer">
<ul>
<li id="modal-0" class="team"></li>
<li id="modal-1" class="team"></li>
<li id="modal-2" class="team"></li>
</ul>
</div>

The current way I am trying to make this work (but I understand if this is just completely wrong):

window.onload = function(){ 
  for (let i = 0; i < 10; i++) {
    let teamId = `team-${i}`;
    let teamIndex = document.getElementById(teamId);

    let modalId = `modal-${i}`;
    let modalIndex = document.getElementById(modalId);

    teamIndex.onclick = function() {
      modalIndex.style.display = "block";
    }
  }
};

Thank you for any responses and let me know if you need more information.

4

You can toggle display using a helper class. If you want the “modals” to all be invisible then initially add a class that sets their display to none or just change in your CSS.

I added buttons to the list items for the teams as buttons/inputs are for clicks events. Use the event.target of the button to travel up the DOM and get a parent element to query the modal elements list.

Query over the team classes and get the closest div element for the team buttons and then get its parentNode, use that to query the modal groups div ul listItems => #modalContainer li. Iterate over them and use the index in the forEach loop to add a helper class to show the proper modal.

I have added a basic snippet showing this logic and further comments in the snippet. Let me know if anything is unclear or if this is not what you were looking for.

NOTE: Keep in mind using this logic would mean your listItems for
teams and modals must have the same amount of index and each index
would need to match.

Using something like a data-attribute => <li id="modal-1" class="team" data-team-id='team-1'> in your ‘modal‘ would be a more robust approach. Then you could query on a click eventListsner using something like e.target.querySelector('[data-modal-id="${e.target.id}"]')

const teams = document.querySelectorAll('.team button');
const modals = document.querySelectorAll('#modalContainer li');

// set your "modals" to display of none initially
modals.forEach(modal => {
  modal.classList.add('modals');
})

// loop over the teams
teams.forEach((team, i) => {
  // click event listener passing in event
  team.addEventListener('click', (e) => {
    // get the parent of the button groups div
    const parent = e.target.closest('div').parentNode;  
    // get the modals using the team buttons parent element
    const modalList = parent.querySelectorAll('#modalContainer li');
    // iterate over the modal elements and remove any `show` helper class
    modalList.forEach(list => {
      list.classList.remove('show');
    });
    // get the corresponding modal for the teams index from forEach methods index param
    const target = modals[i];
    // add the show class to view the modal that matches your teams index
    target.classList.add('show');
  });
})
.modals {
  display: none;
}

.show {
  display: block;
}
<div id="teamContainer">
  <ul>
    <li id="team-0" class="team">
      <button>Team 1</button>
    </li>
    <li id="team-1" class="team">
      <button>Team 2</button>
    </li>
    <li id="team-2" class="team">
      <button>Team 3</button>
    </li>
    <li id="team-20" class="team">
      <button>Team 20</button>
    </li>
    <li id="team-999" class="team">
      <button>Team 999</button>
    </li>
  </ul>
</div>

<div id="modalContainer">
  <ul>
    <li id="modal-0" class="team">Modal for team 1</li>
    <li id="modal-1" class="team">Modal for team 2</li>
    <li id="modal-2" class="team">Modal for team 3</li>
    <li id="modal-20" class="team">Modal for team 20</li>
    <li id="modal-999" class="team">Modal for team 999</li>
  </ul>
</div>

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