Skip to content
Advertisement

Create new dynamic id in javascript

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

CSS

JavaScript

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

css should also changed too

here is working fiddle : https://jsfiddle.net/0fhykzmw/

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement