Urgent, Need Help with localStorage Saving

Screen Shot 2020-07-18 at 09.38.44

Im trying to build a chrome ‘new tab’ extension, and im trying to make the user input their name into a form, and there will be a greeting (ex. Good Afternoon + name) after a button next to the input field is clicked, using an onclick event which executes the function above . But when i try my code it doesn’t work (the form does not dissappear) , and I do not know how to make the form appear only the first time its being loaded, and not after other reloads. Any help would be greatly appreciated.

Hi,
As I understand you, what you are trying to do is two things:

  1. make the form disappear after the greeting is displayed.
  2. only show the form when the user has not already entered a name.

For the second of these, perhaps you might write a function to see if there is anything stored in local storage when the page is first loaded. In other words, only show the form when the following condition is met:

if (localStorage.getItem('names') === null)

For the first did you try setting style.display = 'none' on the element you are trying to hide within the function that sets the name in local storage? In other words, when the user enters her or his name, set it into local storage and then turn the display value on the form to none.

That’s just a couple of thoughts from me. Not sure if it will help because I am not quite sure exactly what you are trying to do, given your description and short bit of code.

Thanks man, your solution worked perfectly!

Awesome, glad to help! I am learning Javascript myself at the moment, so I am also happy I didn’t send you in the wrong direction. And welcome to the forums!

  • George