Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

238
Views
How do you update input text value based on user input and display the value? (CRUD app update/save feature)

I'm currently building a CRUD app so users can keep track of books they have read or haven't read. Im currently trying to implement an update/edit feature so the user can update the book info if he/she wishes. The issue I'm having is, whenever I try to save the book info after changing the input values, the last book in 'myLibrary' array would get the info instead of the current book I'm trying to update. The only time the current book gets the user input is when it's the only book in the array :(

I tried using the array splice() method to replace the current book with a new book that has the new info/values but I'm still having the same problem.

// book class: represents a book

class Book {
  constructor(title, author, pages, read) {
    this.title = title;
    this.author = author;
    this.pages = pages;
    this.read = read;
  }
}

// array to store books.. default books using the Book Constructor

let myLibrary = [{
    title: 'Dark Matter',
    author: 'Blake Crouch',
    read: 'read',
    pages: 342,
  },
  {
    title: '1984',
    author: 'Geroge Orwell',
    read: 'not read',
    pages: 328
  },
];

/* saves the updated user input on click and closes modal
removes the book from the library array and adds the updated book to the library array and saves it to local storage again with updated info from user input in the form fields in the modal window and closes the modal window after submission of the form fields */

saveBtn.addEventListener('click', (index) => {
  myLibrary.splice(myLibrary.indexOf(index), 1);
  addBookToLibrary();
  setData();
  render();
  document.querySelector('.modal').classList.add('modal--hidden');
  hideBackgroundFade();
  clearForm();
});

clip of the UI... showing the bug

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Difficult to say w/o more code, esp. the HTML structure or a framework you might use. So assuming the following HTML structure:

    <div class='books'>
        <div book-id="123">
            <h1>Title 1</h1>
            <p>200 pages</p>
        </div>
        <div book-id="456">
            <h1>Title 2</h1>
            <p>100 pages</p>
        </div>
    </div>

To be most generic, i.e. any click would link your UI to the data source, your script might could look sg like:

  const books = document.querySelector('.books');
  books.addEventListener("click", evt => {
      const book = evt.target.closest("[book-id]");
      if (book) {
          const bookId = book.getAttribute("book-id");
          const bookInLib = myLibrary.find( elem => elem.id == bookId )
          // here you update your book and trigger rendering
      }
  })
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!