javascript – How to modify checked status of user input on individual card without modifying all cards (when using a for loop to run through an array)

Library project. When I add a new book to my library, the newest book’s read status changes all the book’s read statuses to the latest instead of only modifying the book that is currently being added. I believe this is because I remove all the cards at the beginning of the displayBooks() function. I do this to avoid duplicates when the for loop runs through the library again. I am thinking there is a way to either prevent the for loop from adding any duplicates so that I don’t need to run the removeCards() function or that I can somehow store the read status of each card so that it won’t change when a new book is added.

I’ve tried breaking apart the individual book elements into an array so that I could store the read status values ​​but then I started running into other issues and made somewhat of a mess. Here is my previous code from before I made those changes. The read status is the only issue I am having. Hoping someone can point me in the right direction.

JS

let addBtn = document.getElementById('addBtn');
let cards = document.querySelector('.cards');

let modal = document.getElementById("myModal");
let openModal = document.getElementById("openModal");
let span = document.getElementsByClassName("close")[0];

openModal.onclick = function() {
  modal.style.display = "grid";
}

span.onclick = function() {
  modal.style.display = "none";
}

window.onclick = function(event) {
  if (event.target == modal) {
    modal.style.display = "none";
  }
}

let myLibrary = [];

function getBookFromInput() {
    const title = document.getElementById('title').value;
    const author = document.getElementById('author').value;
    const pages = document.getElementById('pages').value;
    const newBook = new Book(title, author, pages, read);
    newBook.info();
}

function Book(title, author, pages, read) {
    this.title = title;
    this.author = author;
    this.pages = pages;
    this.read = read;
    this.info = function() {
        let book = `Title: ${title}nAuthor: ${author}nPages: ${pages}`
        addBookToLibrary(book);
    }
}

function addBookToLibrary(book) {
    myLibrary.push(book);
    displayBooks();
}

function displayBooks() {
    removeCards()
    
    let read;
    if (document.getElementById('read').checked == true) {
        read = 'Read!';
    } else {
        read = 'Not read yet!';
    }

    let books = myLibrary;
    for (let book of books) {
        let card = document.createElement('div');
        cards.appendChild(card).innerText = book;
        card.classList.add('card');
        card.setAttribute('id', myLibrary.indexOf(book));

        let readBtn = document.createElement('button');
        card.appendChild(readBtn).innerText = read;
        readBtn.classList.add('readBtn');
        readBtn.setAttribute('id', 'readBtn')
        readBtn.onclick = function() {
            if (readBtn.innerText === "Read!") {
                readBtn.innerText = "Not read yet!";
              } else {
                readBtn.innerHTML = "Read!";
              }
        }
        
        let removeBtn = document.createElement('button');
        card.appendChild(removeBtn).innerText="Remove";
        removeBtn.classList.add('removeBtn')
        removeBtn.setAttribute('id', 'removeBtn');

        removeBtn.onclick = function() {
            myLibrary.splice(myLibrary.indexOf(book),1);
            displayBooks();
        }
    }
}

function removeCards() {
    while (cards.lastChild) {
        cards.removeChild(cards.lastChild);
    }
}

HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Library</title>
    <link rel="stylesheet" href="style.css">
    <script src="script.js" defer></script>
</head>
<body>
    <div class="header">
        <h1>Library</h1>
    </div>
    <div class="container">
        <button id="openModal">+</button>
        <div class="modal" id="myModal">
            <div class="modal-content">
                <span class="close">&times;</span>
                <div class="title">
                    <label for="title">Title:</label>
                    <input id="title" type="text">
                </div>
                <div class="author">
                    <label for="author">Author:</label>
                    <input id="author" type="text">
                </div>
                <div class="pages">
                    <label for="pages">Pages:</label>
                    <input id="pages" type="number">
                </div>
                <div class="read">
                    <label for="read">Read?</label>
                    <input id="read" type="checkbox">
                </div>
                <button id="addBtn" onclick="getBookFromInput()">Add Book</button>
            </div>
        </div>
        <div class="cards"></div>
    </div>
</body>
</html>

CSS

:root {
    font-size: 16px;
    font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif;
}

body {
    background-color: whitesmoke;
    display: flex;
    flex-direction: column;
    margin: 0;
}

.container {
    display:flex;
    flex-direction: column;
    gap: 50px;
    background-color: whitesmoke;
    align-items: center;
}

.header {
    background-color: white;
    border-bottom: 5px whitesmoke inset;
    padding-left: 30px;
}

input {
    width: 300px;
}

.newBook {
    font-weight: 700;
    display: flex;
    flex-direction: column
} 

.cards {
    display: grid;
    grid-template-columns: repeat(4, minmax(300px, 1fr));
    gap: 50px;
}

.card {
    border: solid orangered 5px;
    background-color: orange;
    padding: 20px;
    font-size: 2rem;
    font-weight: 500;
    display: flex;
    flex-direction: column;
} 

#openModal, #addBtn {
    border: solid orangered;
    background-color: orangered;
    color: white;
    padding: 10px;
}

#addBtn {
    justify-self: center;
    margin-top: 10px;
    border-radius: 10px;
    max-width: 200px;
    font-size: 1.25rem;
    font-weight: 700;
}

#openModal {
    border-radius: 50%;
    height: 50px;
    width: 50px;
    font-size: 3rem;
    display: flex;
    justify-content: center;
    align-items: center;
    margin-top: 15px;;
}

#removeBtn {
    display: flex;
    justify-self: flex-end;
    align-self: flex-end;
    margin-top: 15px;
    margin-left: 15px;
    background-color: #fefefe;
    color: black;
    border: solid black 2px;
    font-weight: 600;
}

.readBtn {
    background-color: y;
    display: flex;
    justify-self: flex-start;
    align-self: flex-start;
    margin-left: 0px;
    margin-top: 15px;
    color: black;
    border: solid black 2px;
    border-radius: 10px;
    font-size: 1.5rem;
    font-weight: 600;
    max-width: 200px;
}

.modal {
    display: none; 
    position: fixed; 
    z-index: 1; 
    left: 0;
    top: 0;
    width: 100%; 
    height: 100%; 
    overflow: auto; 
    background-color: rgb(0,0,0); 
    background-color: rgba(0,0,0,0.4); 
}

.modal-content {
    display: grid;
    grid-template-rows: repeat(6, 1fr);
    background-color: #fefefe;
    margin: 15% auto; 
    padding: 20px;
    border: 1px solid #888;
    width: 35%; 
}
  
.close {
    color: #aaa;
    margin-left: 95%;
    font-size: 28px;
    font-weight: bold;
}
  
.close:hover, .close:focus {
    color: black;
    text-decoration: none;
    cursor: pointer;
}

.pages, .title, .author, .read {
    display: grid;
    grid-template-columns: 1fr 5fr;
    padding: 10px;
}

label {
    font-weight: 700;
}

Leave a Comment