I made a form with Symfony. I wanted to make sure that select tag can be duplicated. So I made it possible in JavaScript code. I succeeded to give the name I wanted to new selects. But I would like to change the name of the first select also, replace user[wishedlanguages][name] by user[wishedlanguages][0].
I tried .setAttibute() and .replace() and had something like this :
wishedlanguageselect.setAttribute('name', wishedlanguageselect.name.replace('__name__', '0'))
but it didn't work.
JS code :
idcounter = 1;
const wishedlanguageselect = document.getElementById("user_wishedlanguages___name__");
/* wishedlanguageselect.setAttribute('name', wishedlanguageselect.name.replace('__name__', '0')) */
const wishedlanguageslist = document.getElementById('wishedlanguageslist');
document.getElementById("addwishedlanguage").onclick = function() {
idcounter++
const newwishedlanguageselect = wishedlanguageselect.cloneNode(true);
newwishedlanguageselect.setAttribute('name', 'user[wishedlanguages][' + idcounter + ']')
document.getElementById('wishedlanguageslist').appendChild(newwishedlanguageselect);
document.getElementById("removewishedlanguage").style.display = "initial";
}
document.getElementById("removewishedlanguage").onclick = function() {
idcounter--
wishedlanguageslist.removeChild(wishedlanguageslist.lastElementChild);
if(idcounter === 1) {
document.getElementById("removewishedlanguage").style.display = "none";
}
}
