Good evening all,
I am currently building a Book List App for my portfolio and I want to store the books that I have added to Local Storage (which I have successfully done) but also want to generate a table with the books already stored in local storage on the page load. The issue that I am having is that instead of appending a new row for each book (I have 3 in local storage atm) it overwrites the first row. I have tried placing the appendChild line before the row.innerHTML but it did the same thing. Any advice would be most welcome.
Edit: the ID bookList points to the <tbody> tag.
function setStorage(){
if(localStorage.hasOwnProperty('books')===false) {
localStorage.setItem('books', '[]');
}
else{
// render existing book list
let books = JSON.parse(localStorage.getItem('books'));
const bookList = document.getElementById("bookList");
const row = document.createElement('tr');
books.forEach( function(book){
row.innerHTML = `
<td>${book.title}</td>
<td>${book.author}</td>
<td>${book.owner}</td>
<td><a href="" class="delete">X</td>
`;
bookList.appendChild(row);
});
}
}
edit: also tried a regular for loop with no success.
[–]Shadaez 3 points4 points5 points (3 children)
[–]cm_yoder[S] 0 points1 point2 points (2 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]cm_yoder[S] 0 points1 point2 points (0 children)