My current library project involves looping through an array of objects to display them on a grid based on input values. Here is the code snippet for my loop:
const addBook = (ev) => {
ev.preventDefault();
let myLibrary = [];
let bookInfo = {
title: document.getElementById('title').value,
author: document.getElementById('author').value,
pages: document.getElementById('pages').value,
}
myLibrary.push(bookInfo)
for (i = 0; i < myLibrary.length; i++) {
console.log(myLibrary)
var container = document.getElementById('book-shelf')
var div = document.createElement('div')
div.classList.add('cell')
container.appendChild(div);
}
var submitBtn = document.querySelector('.submit-btn');
submitBtn.addEventListener('click', addBook)
However, I'm facing an issue where every time I enter title, author, and pages values and click the submit button, it adds multiple cells instead of just one. For example, entering the information once gives me 3 cells, entering it again gives me 6 cells, and so on. How can I change my code to ensure that only one cell is added each time I submit a new book?