0

Given the following json:

    var obj = [
{
  "Name": "John",
  "id": 1
},{
  "Name": "Mike",
  "id": 2
},{
  "Name": "Maria",
  "id": 3
},{
  "Name": "Jen",
  "id": 4
},{
  "Name": "Ben",
  "id": 5
}
]

I outputted all the Names of the json file through a for loop.

for (var i = 0; i < obj.length; i++) {

   output+= obj[i].Name+' Rename ' + '<button onclick="dublicateF(i)">Dublicate</button>' + ' Delete'+ '<br>';

}

How do I make sure that the Duplicate button that I click actually duplicates the element (and outputs it real time)? The i value that gets transfered to the dublicateF function is always equals to 5. (the length/size of the json elements)

I tried following steps from this question which is similar: - but it outputs all the Buttons next to each other instead of next to each respective element.

Obviously as you can tell, later I will have to do the same with delete and rename, but I suppose that if I figure out the duplicate, the others wont be much different.

Thanks

  • `i` is not a variable in your string....it will go into the html as that literal letter not the value of your variable. Use string concatenation as you are doing with the other variable .... `dublicateF(' + i + ')` – charlietfl Jun 20 '20 at 19:16
  • Okay wow this solved it. The whole thing was just a 'i'and I was trying all sort of things haha. Thank you! – Panayiotis Jun 20 '20 at 19:40
  • Same reasoning that the word `"Dublicate"` or `"onclick"` don't change when they contain `i`. When inside quotes they are literal letters – charlietfl Jun 20 '20 at 19:44

0 Answers0