0

I am looping through an array like this:

<% thoughts.docs.forEach(function(thought, i) { %>
    <div class="square-box-container">
        <div class="pyp-image-container">
            <div class="image-overlay">

By default, element class name 'image-overlay' is hidden with display: 'none'. I am trying to create a function with an onclick event, so when user clicks on div 'square-box-container', the image overlay for that element only changes to display: 'block'.

Currently I have the below code but I think my inner loop in wrong, as when I click on a square box container, the image overlays for ALL the square box containers change to display: 'block', as opposed to that container overlay only. Can anyone advise what I'm doing wrong please?

  var containerItems = document.getElementsByClassName("square-box-container");
  var overlayItems = document.getElementsByClassName("image-overlay");

  for (var i = 0; i < containerItems.length; i ++) {
      containerItems[i].onclick = function() {
        for (var i = 0; i < overlayItems.length; i ++) {
          overlayItems[i].style.display = 'block';
        }
      }
  }

I'm not very familiar with use of child nodes, is that what is required here? Thanks

1 Answer 1

1

If you want only the associated element to have its display changed, don't loop inside the click handler - and use let instead of var.

for (let i = 0; i < containerItems.length; i++) {
    containerItems[i].onclick = function () {
        overlayItems[i].style.display = 'block';
    }
}

Another option is to omit the overlayItems collection entirely, and navigate from the clicked element instead.

for (const container of document.getElementsByClassName("square-box-container")) {
    container.addEventListener('click', (e) => {
        e.currentTarget.querySelector('.image-overlay').style.display = 'block';
    });
}
Sign up to request clarification or add additional context in comments.

3 Comments

Exactly what I was looking for, thanks! Actually I had already tried the first solution and couldn't understand why it wasn't working, but seems it was due to using var instead or let. Thanks again!
Hi I have a follow up question on this. Although the above answer works well per my original query, I've found that as I'm also using Mongoose pagination, it only appears to work on page 1 of the pagination (first 20 records). Once I move to page 2 (next 20 records), the onclick function is no longer working. Is there a way to ensure the function continues to works with all records?
Use event delegation instead. Add a single listener to the container, and when it's clicked, find the .closest('.square-box-container'). If it exists, find the descendant .image-overlay and toggle it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.