I am looping through an array and appending the result to an HTML element on my page.
Here is my JavaScript/jQuery:
var numberOfFiles = email.file_id.length;
for (var fileIndex = 0; fileIndex < numberOfFiles; fileIndex++) {
fileHTML += '<a href="downloadAttachment.php?file_id=' + email.file_id[fileIndex] + '" target="_blank">' + email.file_name[fileIndex] + '</a><br>';
emailOpened.find('.file-attachment-wrapper').append(fileHTML);
}
The loop works and appends the content to my HTML, however, there is a small bug. The first item in my array is appended twice.
Here is what my array looks like before the loop:
["file1.png", "file2.pdf"]
After the loop is completed, the resulting HTML looks like this:
<a href="downloadAttachment.php?file_id=54d38004fdf902cb758b456a" target="_blank">file1.png</a><br>
<a href="downloadAttachment.php?file_id=54d38004fdf902cb758b456a" target="_blank">file1.png</a><br><a href="downloadAttachment.php?file_id=54d38004fdf902cb758b456b" target="_blank">file2.pdf</a><br>
As you can see, the file1.png is being added twice. Why is this happening?