Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

110
Visualizações
Using javascript to save a text entry to local storage

I am trying to use javascript to save a text entry using local storage with little success could anyone offer any advice please as i'm new to javascript. It is for a basic blog post site where the user submits text into a diary entry. I'm particularly unsure about how to get the textarea elements current value as shown in the code below and think that is where im going wrong.

function addTextEntry(itemKey, initialText, isNewEntry) {
  // Create a textarea element to edit the entry
  var textElement = document.createElement("textarea");
  textElement.rows = 5;
  textElement.placeholder = "(new entry)";

  textElement.value = initialText;

  addSection(itemKey, textElement);

  // If this is a new entry (added by the user clicking a button)
  // move the focus to the textarea to encourage typing
  if (isNewEntry) {
    textElement.focus();
  }

  // Create an event listener to save the entry when it changes
  function saveEntry() {
    console.log("saveEntry called with variables in scope:", {
      itemKey,
      initialText,
      isNewEntry,
      textElement,
    });


    // Save text entry
    // ...get the textarea element's current value
    textElement = document.getElementById;
    // ...make a text item using the value
    textElement.value = initialText;
    //    (demonstrated elsewhere in this file)
    // ...store the item in local storage using the given key
    localStorage.setItem(itemKey);
  }

  // Connect the saveEntry evenFt listener to the textarea element 'change' event
  textElement.addEventListener("change", saveEntry);
  // (demonstrated elsewhere in this file)
}

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Looks like you're using getElementById wrong.

Try the following to get the current value of a <textarea>:

function logTextareaValue () {
  const element = document.getElementById('myTextarea')
  const value = element.value
  console.log(value)
}
<textarea id="myTextarea">Default text</textarea>
<button onClick="logTextareaValue()">Click to log textarea value to console</button>

about 4 years ago · Juan Pablo Isaza Relatório

0

In you code, you are passing only one parameter itemKey to the setItem() method. You should also pass the value for that key as a second parameter.

localStorage.setItem(itemKey, itemValue);
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda