Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set div id's from array content

I'm trying to figure out how to give 16 different divs the id names that are stored in a 16 elements long array.

This is so that I can randomize the divs "placement" for a memory game, since there will be 8 different div styles that will change along with the div id if that is possible.

My plan is to have the same name for the div id as for the style for that specific div.

Is there any way to set the first div's id and style as the value in myarray[0], and the second div's id and style as myarray[1], and so on?

EDIT:

var card = ["orange","orange","pink","pink","red","red","purple","purple",
"blue","blue","green","green","brown","brown","yellow","yellow"];

for(var j, x, i = card.length; i; j = parseInt(Math.random() * i),
x = card[--i], card[i] = card[j], card[j] = x);

then later in the body I'm trying to achieve something that represents this:

<div id="card[0]"></div>
<div id="card[1]"></div>
<div id="card[2]"></div>

and so on...

like image 626
Dave Avatar asked Jan 28 '26 07:01

Dave


1 Answers

Here is a solution for randomising class names using pure JavaScript.

Updated answer

I have updated my solution now that the question was clarified, here is it adapted to your colors. I have set the background-color of the .cards to the colors set in the array. This could easily be done using the id as well, I recommend against using [] characters in an id though as I think I'm not sure if that's standards compliant.

jsFiddle

enter image description here

var colors = [
    "orange","orange","pink","pink","red","red","purple","purple",
    "blue","blue","green","green","brown","brown","yellow","yellow"
];

var divs = document.getElementsByClassName("card");

while (divs.length > 0) {
    var i = Math.floor(Math.random() * colors.length);
    divs[0].style.backgroundColor = colors[i];
    colors.splice(i, 1);
    divs = [].slice.call(divs, 1);
}

Original answer

Given an array of ids and a set of HTML elements, a random id will be assigned to each element from ids.

jsFiddle

enter image description here

JavaScript

var ids = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"];
var divs = document.getElementsByClassName("game-element");

while (divs.length > 0) {
    var i = Math.floor(Math.random() * ids.length);
    divs[0].id = 'item-' + ids[i];
    ids.splice(i, 1);
    divs = [].slice.call(divs, 1);
}

HTML

<div class="game-element"></div>
<div class="game-element"></div>
<div class="game-element"></div>
<div class="game-element"></div>
<div class="game-element"></div>
<div class="game-element"></div>
<div class="game-element"></div>
<div class="game-element"></div>
<div class="game-element"></div>
<div class="game-element"></div>

CSS

.game-element {
    width:10px;
    height:10px;
    float:left;
}

#item-1  { background-color:#F00; }
#item-2  { background-color:#0F0; }
#item-3  { background-color:#00F; }
#item-4  { background-color:#FF0; }
#item-5  { background-color:#F0F; }
#item-6  { background-color:#0FF; }
#item-7  { background-color:#A0A; }
#item-8  { background-color:#0AA; }
#item-9  { background-color:#AA0; }
#item-10 { background-color:#000; }
like image 106
Daniel Imms Avatar answered Jan 30 '26 20:01

Daniel Imms