I am creating a small program that creates some divs for me and when I create a new div a counter is increased, to have a unique id in each one, the problem is that it is only increased once and then the new ones that I create they stay with the same id.
This is my JavaScript:
function duplicate(){
var i = 0;
i++
let campos = document.createElement('div');
campos.id = 'id' + i ;
document.getElementById('duplicados').appendChild(campos);
}
For what it's worth, this is my html:
This is the button with which I activate the function
<center><input type="button" id="add" onclick="duplicate()" value="Añadir"></center>
And the new div I insert them into a div called "duplicates"
Juan Manuel, one way to solve it as you have it would be using classes (they always come in handy for this and other things like styles, etc.). I propose something like this: