Is it possible to change the index of the last item of a multidimensional input when my new item is created ?
Let me explain.
I have a button that is clone an item from my list, into my list. Each item contains several properties (date, category…)
This is an example of what I have when I display my form :
validation_form[classe][0][matieres][11][ressources][XXXXX][hasBeenRenewed]
What I do is that I clone all my item, but for now, the index XXXX is not updated.
How can I update the XXXX directly when I clone my item ? I thought I could count the number of items, and then change the index, but this would be painful and time consuming…
Advertisement
Answer
Use a regex in match()
to create array of them then update the one you want and join() back together
const el = document.querySelector('input'); const prefix = el.name.split('[')[0], ind = el.name.match(/[(.*?)]/g); ind[5]= `[AAA]`; el.name = `${prefix}${ind.join('')}` console.log(el)
<input name='validation_form[classe][0][matieres][11][ressources][XXXXX][hasBeenRenewed]'/>