Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3500462
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T12:44:12+00:00 2026-05-18T12:44:12+00:00

I have a loop of 50 divs, in which I am assigning event based

  • 0

I have a loop of 50 divs, in which I am assigning event based on the current element. However I want to assign event based on a condition.

For example, divs are arranged one behind another, just like a photo stack.
I want user to first hit first div, then only they should able to hit second div then third div and so on till they reach the end of divs.

Because of the below code user can hit on any of the div and perform action. But action needs to be performed one by one. One div after another. Please suggest the solution.

var flag=0;
var flipImage=document.querySelectorAll('.shadow');  
   for(j=0; j<flipImage.length; j++){
   var currentFlipImage=flipImage[j];
      if(j==flag){
      flag++; 
      currentFlipImage.addEventListener('click',flipDown,false);
     }
  }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T12:44:13+00:00Added an answer on May 18, 2026 at 12:44 pm

    Maybe something like:

    var _handler = null;
    var images = document.querySelectorAll('.shadow');
    var length = images.length;
    
    function attachHandler(index) {
        if(_handler) {
            // remove the click handler from the previous image
            images[index-1].removeEventListener('click', _handler, false);
            _handler = null;
        }
        if(index < length) {
            var handler = function(event) {
                flipDown(event);
                attachHandler(index+1); // add the click handler to next image
            }
            images[index].addEventListener('click', handler, false);
            _handler = handler;  // capture reference for removal
        }
    }
    
    attachHandler(0);
    

    This only generates and adds event listeners when needed.

    Important side note: The return value of querySelectorAll() is a NodeList which is live. That means any time you call length on this object, the list is reevaluated (the elements are searched again) which adds a performance overhead. Thus, never use list.length in the for loop. Capture the length beforehand.

    Working DEMO

    If you want to make it going in a loop, just remove the if statement (keep the body of course) and do

    index = index % length;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have little piece of javascript to show/hide divs based on which link is
I have a loop MC which will be duplicate to stage several times according
I have an application creating a bunch of divs through a loop. Each div
I have a collection of div's which I need to remember and loop through,
I have some divs created dynamically this way: //here goes some loop, and everything
I have divs created within a while loop displaying a MySQL query. I'd like
I have a table that's generated by a normal PHP loop. What I want
I have a function that displays a few divs running through a loop using
hey guys I have two divs- with id=form1 which is inline and the other
Okay so we have a mySQL while loop echoing out divs for a certain

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.