Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

176
Vistas
How to store input value in javascript local storage in the form of array?

I am trying to store book name value in local storage when the form is submitted in the form of an array. But bookName variable works, but it changes every time a new book name is entered as the function runs again.

let bookName = document.getElementById("bookName").value;

So i want to store all the book names in the form of an array and i get the following error. Here is code:

let libraryForm = document.getElementById("bookForm");
libraryForm.addEventListener("submit", libraryBooksDetails);

function libraryBooksDetails(e) {
    
    e.preventDefault();
    console.log("The book details have been submitted");
    let bookName = document.getElementById("bookName").value;
    let bookAuthor = document.getElementById("author").value;
    let bookType;
    let fiction = document.getElementById("Fiction"); 
    let programming = document.getElementById("ComputerProgramming");
    let personal = document.getElementById("PersonalDevelopement");
    
    if(fiction.checked){
        bookType = fiction.value;
    }
    else if(programming.checked){
        bookType = programming.value;
    }
    else if(personal.checked){
        bookType = personal.value;
    }

    let book = new bookDetails(bookName,bookAuthor,bookType);
    
    let bookData;
    bookData = bookData || [];
    let nameOfBooks = bookData.push(book.bookName);
    localStorage.setItem("books",JSON.stringify(nameOfBooks))
    console.log(book);

    let display = new Display()
    display.add(book);
    display.clear();
}

The function I used to store value returns 1. Don't know why?

Any help would be really appreciated.

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda