I have this piece of code right here that creates somewhat of a grid (but not really) of chocolate pieces in x and y positions (height and length) depending on what the user chooses in the input, and i want it to assign a new id (preferably imgPos1, imgPos2 etc…) to each piece of chocolate it produces individually. I have tried referring to other posts but i am completely clueless. I am open to any suggestions to third party frameworks (react etc).
HTML (and js):
JavaScript
x
50
50
1
<html>
2
<body>
3
<div>
4
<form name='grid' id="grid">
5
<!-- Form to define the x size and y size of the grid made out of chocolate pieces -->
6
Desired Length: <input type='number' name='length' min=1 max=10 step=1 value=1 id="lengthInput" />
7
Desired Height: <input type='number' name='height' min=1 max=10 step=1 value=1 id="heightInput" />
8
<input type='button' value='Apply' class="button_Change" />
9
</form>
10
</div>
11
<div id='choc'></div>
12
<template>
13
<img id="imgPos" src="https://cdn.shopify.com/s/files/1/1061/1924/products/Chocolate_Bar_Emoji_large.png?" onclick='removeMe(this)' class='visible'>
14
</template>
15
<script type="text/javascript">
16
/* Script that creates the grid by cloning the chocolate image for x:length
17
and y:height */
18
19
// defines variables q,d,form using jquery
20
const d = document;
21
const q = (e, n = d) => n.querySelector(e);
22
const form = d.forms.grid;
23
24
// Listens for activity on the button depending on its state
25
document.querySelector('[type="button"]').addEventListener("click", (e) => {
26
let choc = q("#choc");
27
choc.innerHTML = "";
28
29
// Maxes out the values for height and length to 10 for practical reasons
30
if (form.height.value > 10) form.height.value = 10;
31
if (form.length.value > 10) form.length.value = 10;
32
33
// assigns chocolate image to length and height
34
for (let y = 0; y < form.height.value; y++) {
35
let div = d.createElement("div");
36
choc.appendChild(div);
37
38
for (let x = 0; x < form.length.value; x++) {
39
div.appendChild(q("#imgPos", q("template").content).cloneNode(true));
40
}
41
}
42
});
43
44
function removeMe(e) {
45
e.classList.remove("visible");
46
};
47
</script>
48
</body>
49
</html>
50
CSS
JavaScript
1
49
49
1
#grid {
2
top: 180px;
3
left: 350px;
4
position: absolute;
5
cursor: auto;
6
font-family: 'Press Start 2P', cursive;
7
font-size: 10px;
8
}
9
#imgPos {
10
display: block;
11
width : auto;
12
height: auto;
13
cursor: pointer;
14
position: relative;
15
width: 20px;
16
height: auto;
17
top: 0px;
18
left: 0px;
19
}
20
#choc {
21
display: grid;
22
grid-template-rows: 1fr 1fr;
23
margin-left: auto;
24
margin-right: auto;
25
top: 240px;
26
left: 340px;
27
position: absolute;
28
cursor: default;
29
}
30
31
#choc > div{
32
position: absolute;
33
width:50px;
34
display:block;
35
}
36
37
#choc > div > #imgPos{
38
float: left;
39
float: right;
40
clear:none;
41
width:50px;
42
display: inline-block;
43
}
44
45
#choc > div > #imgPos:not(.visible) {
46
opacity: 0;
47
cursor: default;
48
}
49
Here’s a fiddle I made using it:
https://jsfiddle.net/p4ekmtgh/
Advertisement
Answer
You can concat the id with variable x and y
JavaScript
1
4
1
let child = q("#imgPos", q("template").content).cloneNode(true);
2
child.setAttribute("id","imgPos" + x + "_" + y);
3
div.appendChild(child);
4
css should also changed too
here is working fiddle : https://jsfiddle.net/0fhykzmw/