I am trying to code Memory tiles games with JS, HTML and CSS. I am adding difficulty level easy(44 tile), med(66 tile), and hard(88 tile). When I select 66 , I need to add block of code to html file by DOM.
<div class="tiles" data-name="8">
<img class="original" src="images/front-image.png" />
<img class="shuffled" src="images/logo_8.jpg" />
</div>
I need to do this many times as for easy level, I need 16 tiles in total, for med level 32 tiles and for hard level 64 tiles.
Can anyone help me to add inner html for this?? I need to add innerHTML before section part end.
<section class="game-tiles">
<div class="tiles" data-name="1">
<img class="original" src="images/front-image.png" />
<img class="shuffled" src="images/logo_1.png" />
</div>
<div class="tiles" data-name="1">
<img class="original" src="images/front-image.png" />
<img class="shuffled" src="images/logo_1.png" />
</div>
<div class="tiles" data-name="2">
<img class="original" src="images/front-image.png" />
<img class="shuffled" src="images/logo_2.png" />
</div>
<div class="tiles" data-name="2">
<img class="original" src="images/front-image.png" />
<img class="shuffled" src="images/logo_2.png" />
</div>
<div class="tiles" data-name="3">
<img class="original" src="images/front-image.png" />
<img class="shuffled" src="images/logo_3.png" />
</div>
<div class="tiles" data-name="3">
<img class="original" src="images/front-image.png" />
<img class="shuffled" src="images/logo_3.png" />
</div>
<div class="tiles" data-name="4">
<img class="original" src="images/front-image.png" />
<img class="shuffled" src="images/logo_4.jpg" />
</div>
<div class="tiles" data-name="4">
<img class="original" src="images/front-image.png" />
<img class="shuffled" src="images/logo_4.jpg" />
</div>
<div class="tiles" data-name="5">
<img class="original" src="images/front-image.png" />
<img class="shuffled" src="images/logo_5.png" />
</div>
<div class="tiles" data-name="5">
<img class="original" src="images/front-image.png" />
<img class="shuffled" src="images/logo_5.png" />
</div>
<div class="tiles" data-name="6">
<img class="original" src="images/front-image.png" />
<img class="shuffled" src="images/logo_6.jpg" />
</div>
<div class="tiles" data-name="6">
<img class="original" src="images/front-image.png" />
<img class="shuffled" src="images/logo_6.jpg" />
</div>
<div class="tiles" data-name="7">
<img class="original" src="images/front-image.png" />
<img class="shuffled" src="images/logo_7.png" />
</div>
<div class="tiles" data-name="7">
<img class="original" src="images/front-image.png" />
<img class="shuffled" src="images/logo_7.png" />
</div>
<div class="tiles" data-name="8">
<img class="original" src="images/front-image.png" />
<img class="shuffled" src="images/logo_8.jpg" />
</div>
<div class="tiles" data-name="8">
<img class="original" src="images/front-image.png" />
<img class="shuffled" src="images/logo_8.jpg" />
</div>
</section>
And the JavaScript:
const gameTiles = document.querySelector(".game-tiles");
gameTiles.insertAdjacentElement = `
<div class="tiles" data-name="1">
<img class="original" src="images/front-image.png" />
<img class="shuffled" src="images/logo_1.png" />
</div>`;
its giving me error
2
Answers
I didn’t fully understand your problem, but you should try using a loop in JavaScript to do this.
For a better quality answer, you should separate your
html
, andjavascript
code, and give us at least the error you receive, and the expected result!Take for example the code below that performs the following task:
The
generateTiles
function takes as parameters, the number of tiles to generate, and the number of the last assigned tile, as well as the HTML DOM element in which to add the tiles.since insertAdjacentElement is a method you should try this syntaxe
position argument can take some values , you can find more here