I am trying to display object values on a new table row but i get "undefined" instead.
the user inputs information of a book in a form on the html and my aim is to display it using a button on-click "displayBooks()"
here's the JavaScript with the "displayBooks()" function i'm having trouble with
//array to store the books
let books = [];
//book constructor
class Book {
constructor(title, author, genre, review) {
this.title = title;
this.author = author;
this.genre = genre;
this.review = review;
}
}
//creates new object from values input in html
function createBook() {
let book = new Book(
document.getElementById("title").value,
document.getElementById("author").value,
document.getElementById("genre").value,
document.getElementById("review").value
)
console.log(book);
books.push(book);
localStorage.setItem("Books", JSON.stringify(books));
displayBooks();
}
function displayBooks() {
//getting the object from local storage
let books = JSON.parse(localStorage.getItem("Books")); console.log(books);
let table = document.getElementById("table");
var newRow = table.insertRow(table.length),
cell1 = newRow.insertCell(0),
cell2 = newRow.insertCell(1),
cell3 = newRow.insertCell(2),
cell4 = newRow.insertCell(3)
title = books.title,
author = books.author,
genre = books.genre,
review = books.review;
cell1.innerHTML = title;
cell2.innerHTML = author;
cell3.innerHTML = genre;
cell4.innerHTML = review;
}