Is it possible to modify the color of each HTML division individually, with a delay of 500 milliseconds for each

I have been struggling with dom manipulations in a sorting visualizer game I am developing using Vanilla JS and HTML/CSS3. Specifically, I am having difficulty implementing color changes for each div at a particular time interval of 500ms. How can I successfully change the color of each division within this specified time frame?

  <body>
    <div class="color">Division 1</div>
    <div class="color">Division 2</div>
    <div class="color">Division 3</div>
    <div class="color">Division 4</div>
    <div class="color">Division 5</div>
  </body>
  <script>
    var color = document.querySelectorAll(".color");
    console.log(color[0]);

    setTimeout(change, 3000);

    function change() {
      color[0].style.background = "green";
    }
  </script>
  <style>
    div {
      height: 100px;
      width: 100px;
      border: 1px solid black;
      overflow: hidden;
      background-color: aqua;
    }
  </style>

Answer №1

Instead of utilizing JavaScript to modify the background color, consider creating a CSS class with your desired styling. Then, employ querySelector to select the initial color div that does not have the updated green class. Once all the divs have been updated, the interval will automatically clear itself.

setInterval(change, 3000);

function change() {
  const el = document.querySelector(".color:not(.green)");
  if (el) {
    el.classList.add('green');
  } else {
    clearInterval();
  }
};
.color {
    height: 100px;
    width: 100px;
    border: 1px solid black;
    overflow: hidden;
    background-color: aqua;
}
.green {
  background-color: green;
}
<body>
    <div class="color">Division 1</div>
    <div class="color">Division 2</div>
    <div class="color">Division 3</div>
    <div class="color">Division 4</div>
    <div class="color">Division 5</div>
  </body>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What steps can be taken to ensure that an element does not exceed the boundaries of its grandparent container?

I am facing a unique scenario where I have a div with dynamic content, and inside it is another div containing a growing list. The challenge is to allow the internal div to expand until the root div reaches its maximum height, after which overflow should ...

Creating an animated Gif using HTML and CSS styling

I am trying to create an effect where a .gif animation plays once when the mouse hovers over a specific image. I have one static image in PNG format and one animated gif. The goal is for the gif to play every time the mouse is hovered over the png image. ...

Navigation panel featuring points of interest

Greetings, my name is Kasper! I am currently working on incorporating waypoints into the sidebar of my portfolio website, a feature I just recently discovered. However, I seem to be struggling with getting it right. Here is a snippet of my code: < ...

browsing the dropdown when it loses focus

My form includes a date of birth field displayed in 3 dropdowns for Month, Date, and Year. I am currently trying to validate these dropdowns based on their classes rather than individual IDs. Previously, validations were done based on the ID for each of th ...

Easy way to eliminate empty elements following a class using jQuery

I'm encountering a situation where I have a group of elements following a specific class that are either empty or contain only white space. <div class="post-content"> <div class="slider-1-smart"> --- slider contents --- < ...

Form submitted without AJAX rendering will not process data

After implementing an ajax-rendered form triggered by a show form button click, I am facing an issue where the form renders correctly but fails to submit. When attempting to submit the form, there is no response and the server does not receive any data. Be ...

Sequelize associations are not functioning optimally as expected

Trying to display a nested relationship: Cat.hasMany(legs) Leg.belongsTo(cat) Leg.hasOne(paw) paw.hasMany(leg) This is the Cat Model: module.exports = (sequelize, DataTypes) => { const Cat = sequelize.define('Cat', { us ...

Website rendering tearing problem observed in Webkit on iOS browsers

I'm encountering a strange issue with my website's rendering specifically on iOS webview browsers such as Safari, Chrome, and Firefox. This problem only seems to occur on iOS devices and is not replicable on PC, Mac, or Android devices. Initiall ...

Troubleshooting Problem with Google Maps CSS

I'm in the process of constructing a website utilizing Foundation and have integrated Google Maps to showcase some markers on it. However, I've noticed that the map control elements in the top left corner (zoom in and zoom out) have disappeared, ...

Display the <div> element at the current scroll position on the page

Alright, I have a challenging question for you! I don't have the ability to search the internet for this one because I can't quite put it into words. All I have are some images and wild guesses on how it's done. Maybe you'll find it int ...

Tips for customizing the appearance of a specific mat-button component in an Angular project

In my Angular application, I have set up a scenario where users are presented with multiple choices through buttons. When a user clicks on a button, a specific component is displayed based on their choice. I am currently working on enhancing the styling of ...

What is the best way to view and use the data stored within this JSON object?

Obtaining information from a straightforward API (). I retrieve the data using getJSON: var police = $.getJSON(queryurl); A console.log on police displays this: https://i.sstatic.net/Kjyz8.png However, I am unable to access the properties within the o ...

Animating vector graphics from a circle to a line using SVG

Is it possible to convert a circle into a line (path)? I'm having trouble finding information on how to shape the svg element. Perhaps my search technique is the issue. <svg id="Layer_1" data-name="Layer 1" xmlns="http://www.w3.org/2000/svg ...

Upon launching the Selenium test, the default page that is opened is the Chrome settings

As a newcomer to the world of test automation, I am currently honing my skills with Selenium. Having experimented with Selenium Chrome Webdriver in C#, I have encountered an issue. When executing tests, the settings page keeps opening as the default tab i ...

Simulated function invocation just one time

I am testing the functionality of two functions in the same file. One of the functions is supposed to call the other, and I need to verify that this interaction occurs. import * as strings from './strings'; const generateUuidSpy = jest.spyOn(st ...

When using the Actions SDK, you may encounter an issue stating "TypeError: Cannot read property 'output' of undefined"

I've been attempting to link IBM Watson and Google Assistant together, but I keep encountering the error "TypeError: Cannot read property 'output' of undefined" along with "Function execution took 3323 ms, finished with status: 'crash&a ...

The power duo of Three.js and OrbitControl gang up to

Is this a pure javascript mistake or something else? I attached an eventListener in the class function below and received this error: Uncaught TypeError: Cannot read property 'call' of undefined I want to clarify that all the functions being ca ...

Can you help identify the issue in this particular ajax code?

Here is the code I wrote to check if a username exists in the database using Ajax. However, I am facing an issue where the input text from the HTML page is not being sent to the checkusername.php file via $_POST['uname'];. I have tried multiple s ...

What is the method for specifying a JSON object within a DOM element's `data` attribute?

In an attempt to store JSON data within a dataset attribute, I encountered the following HTML structure: The appearance of the HTML is as follows <div data-switch="true" data-json="{'key1': 'value 1'}, {'key2': 'valu ...

Tips for managing the return value of a PHP function in AJAX requests

Looking for some help with inserting HTML form data using PHP and Ajax. Below is the code I've written: <!DOCTYPE HTML> <html lang="en"> <head><title>Ajax Test</title> <meta charset="utf-8" name="viewport" con ...