Home > Back-end >  saving text in local.storage in javascript
saving text in local.storage in javascript

Time:02-05

I'm quite new to coding. My goal is to make a simple "daily planner" page where you can save text in each line. I was trying to do this via local.storage, but I'm not quite sure what I'm doing wrong. I've tried get vs setitem (which I'm not sure if there's a difference) I tried renaming my elements.

  div.text(dailyPlanner[i].hour);

  let textbox = $("<textarea>");
  textbox.attr("data-hour", dailyPlanner[i].line);
  textbox.addClass("col-8 col-md-10 description");
  textbox.val(dailyPlanner[i].text);
  if (currVal !== null) {
    localStorage.setItem(textbox, line);
    let textbox = localStorage.setItem(textbox)
  }

CodePudding user response:

The key needs to be a string so make the key "textbox" and also, what is the value of currVal? Can you confirm it isn't null?

CodePudding user response:

I see two problems in your setIem call:

  1. You are using an html node as a key (textbox) while you should use a string
  2. You are using line as a value, which is undefined in your present code.

So you can try this instead, or adapt this to your case.

localStorage.setItem(dailyPlanner[i].text, dailyPlanner[i].line);
  • Related