More efficient way to update multiple elements in javascript and/or jquery?

Posted by Seiverence on Stack Overflow See other posts from Stack Overflow or by Seiverence
Published on 2012-06-27T21:08:40Z Indexed on 2012/06/27 21:15 UTC
Read the original article Hit count: 142

Filed under:
|

Say I have 6 divs with ID "#first", ""#second" ... "#sixth".

Say if I wanted to execute functions on each of those divs, I would set up an array that contains each of the names of the divs I want to update as an element in the array of strings. ["first", "second", "third"] that I want to update. If I wanted to apply I function, I set up a for loop that iterates through each element in the array and say if I wanted to change 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 if I wanted to update 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 of updating multiple divs without having to sequentially iterate through every element in an array, maybe with some other more efficient data structure besides array? Or is what I am doing the most efficient way to do it?

If can provide some example, that would be great.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery