I'm trying to make a grid that will gonna be controlled by some javascript code.
<div id="blockFrame">
<div id="blockGrid">
<div class="block 0-0">1</div>
<div class="block 0-1">1</div>
<div class="block 0-2">1</div>
<div class="block 1-0">1</div>
<div class="block 1-1">1</div>
<div class="block 1-2">1</div>
<div class="block 2-0">1</div>
<div class="block 2-1">1</div>
<div class="block 2-2">1</div>
<div class="block 3-0">1</div>
<div class="block 3-1">1</div>
<div class="block 3-2">1</div>
<div class="block 4-0">1</div>
<div class="block 4-1">1</div>
<div class="block 4-2">1</div>
</div>
</div>
It's 5x3 table with each cell containing '1' currently. I want all the div
with the class name block
to have a certain size, so I wrote this code, saved as an external js file and made the page load the file. It worked.
var blockSize = document.getElementsByClassName("block");
for (i=0; i<blockSize.length; i++) {
blockSize[i].style.width='2.85em';
blockSize[i].style.height='2.85em';
blockSize[i].style.position='absolute';
}
The problem was in the part which was meant to position the 'blocks' to a certain place. Code below was wrote in the same external js file, but it doesn't do the job.
for (i=0; i<chunkWidth; i++)
for (j=0; j<10*5/chunkWidth; j++) {
eval("document.getElementsByClassName('"+i+"-"+j+"')[0].style.bottom='"+(3.25*5/chunkWidth*j)+"em';")
eval("document.getElementsByClassName('"+i+"-"+j+"')[0].style.left='"+(3.25*5/chunkWidth*i)+"em';")
}
Variable chunkWidth
is defined in another external js files the page will load, and it's a number. I got this error when loading the page.
TypeError: document.getElementsByClassName(...)[0] is undefined
It works if I put the eval code line by line in the browser console, but that's not what I meant (though it works only for divs that were made after loading the page, by writing appendChild into the console, not for divs that were loaded with the page).
What should I do to make this external js file exert the change to the page and all the divs in the page, so I get the blocks arranged?