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

  • Home
  • SEARCH
  • 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 8141365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:29:00+00:00 2026-06-06T12:29:00+00:00

I have 6 divs with ID #first , #second … #sixth . If I

  • 0

I have 6 divs with ID”#first, #second … #sixth.

If I wanted to execute functions on each of these divs, I would set up an array that holding each div name. If I wanted to apply a function, I set up a for loop that iterates through each element in the array and (for example) changes the background color to red:

function updateAllDivsInTheList() {
  for(var i = 0; i < array.size; i++)
    $("#"+array[i]).changeCssFunction();
  }
}

Whenever I create a new div, I would add it to the array.

The issue is, if I have a large number of divs that need to get updated (say 1000 out of 1200 divs), it may be a pain/performance tank to have to sequentially iterate through every single element in the array. Is there some alternative, more efficient way or data structure, of updating multiple divs? Maybe with some other more efficient data structure besides array? Or is what I am doing the most efficient way?

  • 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-06-06T12:29:01+00:00Added an answer on June 6, 2026 at 12:29 pm

    You should be caching your jQuery objects so that your list of names becomes a list of jQuery objects. Iterating over an array of 1000 items is fast. Building 1000 new jQuery objects every time is slow.

    var names = [ "one", "two", ..., "six" ];
    
    // Turn the array of strings into an array of their corresponding jQuery ele
    var $divs = $.map(names, function (v) { return $("#" + v); });
    

    You can also chain multiple ids together into a single selector and avoid the (external) loop; jQuery will (probably) still implement this internally as a loop.

    $('#one, #two, #three').changeCssFunction();
    

    Note that updating the DOM a thousand times is going to be slow, more so in some browsers than in others. You might consider rethinking your code so that you’re adding or removing a class from some parent object, allowing some new CSS selector to match all child elements with a single DOM update.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have multiple DIVs containing an image in each. When I rollover these DIVs,
I have two divs, the first doesn't have much content and the second has
I have two DIVs, first one has a link in it that contains the
I have 2 divs. Second is under first. 2nd div has z-index: -1; top:
I have a layout with a set of divs each containing three fields in
I have 3 divs all floated left. I want to set the second div
http://jsfiddle.net/pC6fC/1/ I have two divs, one above the other. When the first animates, which
I am new to dojo. I have three divs; the first div has 10
I have some jQuery code, which attempts to show the first 6 divs on
I have been working on building my first CSS site using divs and am

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.