I create dinamically a table of emails, and I would like to give to each row of this table an event listener so that I can click on each email. This is my code,
for(var i=0;i<emails.length;i++){
let row = document.createElement('tr');
row.addEventListener('click', () => email(emails[i].id));
let cols = '<td>' + emails[i].recipients + '</td><td>' + emails[i].subject + '</td><td style="width:35%;"> ' + body + '</td><td>'+emails[i].timestamp+'</td>';
row.innerHTML = cols;
document.getElementById('table-body').appendChild(row);
}
Where emails
is a Nodelist.
This is the table:
I'm receiving this error:
Uncaught TypeError: Cannot read property 'id' of undefined
at HTMLTableRowElement.<anonymous> (inbox.js:56)
How can I solve this? Thank you!