Aaron D
Aaron D

Reputation: 49

Filling an array of divs from a javascript array

I have an array of divs, currently 10x10 in html. I also have a 100 big array in Javascript. They look like this.

<div class = "mapArrayA" id = "a1">1</div>
<div class = "mapArrayA" id = "a2">2</div>
<div class = "mapArrayA" id = "a3">3</div>
<div class = "mapArrayA" id = "a4">4</div>
<div class = "mapArrayA" id = "a5">5</div>

//

var mapArray = [];
mapArray[1] = 0; 
mapArray[2] = 0;
mapArray[3] = 0;
mapArray[4] = 0;
mapArray[5] = 0;

The Javascript array will be filled with randomly generated numbers 1-5, with no duplicates using a simple loop. I then want the Javascript array to be visually placed on the divs (as text and eventually CSS modifications) as they follow; a1 should show the value in mapArray[1], and a4 on mapArray[4].

I couldn't find a solution other than I probably have to do something with $(this).text from jquery. Any help would be greatly appreciated.

Upvotes: 1

Views: 247

Answers (1)

Bjorn W
Bjorn W

Reputation: 90

Why don't you just do something like this:

$.each(mapArray, function(key, val){
    $("#a"+key).text(val);
});

If I'm understanding your question correctly.

Upvotes: 1

Related Questions