• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

saving to local storage

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there I've been struggling with this piece of code for a while now and would appreciate any help!
i've changed it in so many ways ive confused myself and im at a loss with it.  

here is the part im stuck with:
 
Saloon Keeper
Posts: 12806
278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So what is the problem?
 
Marshal
Posts: 67510
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You didn't exactly say what the problem is... but I do see a number of issues with the code.

var textareaElement = document.querySelector("textarea");

This selects the first textarea on the page. What if there's another? It'd be better to give it a unique id or class name so that you are sure that you are selecting the desired textarea element.

textareaElement.addEventListener("onblur", saveEntry);

The comment says to listen to the change event, not the blur event. Also, do not include the "on" prefix when naming events.

text = textareaElement.value;

This relies upon a global variable outside of the event listener. This is an extremely poor practice. The target of the event is available in the event construct as target; use that.

var data = text;

Why?

localStorage.setItem(key, item);

What's key? Where does it come from?

console.log("saved item", key, "=", textareaElement.value);

This doesn't prove anything. Fetch the value from local storage to ensure that it's been set.

 
john besty
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:You didn't exactly say what the problem is... but I do see a number of issues with the code.


Thanks for the feedback, ill show you the rest of the code for clarity, my mistake! i've edited it a little but i get an error with the eventlistener and textareaelement, i dont think i need to declare it above but unsure how to correct the error linking it to the saveEntry part.
 
john besty
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This code links to a html file that has post it notes all over and once the user exits the note they've edited, the text should save to local storage.
 
Bear Bibeault
Marshal
Posts: 67510
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

john besty wrote:Thanks for the feedback



You're welcome. How have you acted upon it?
 
john besty
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:

john besty wrote:Thanks for the feedback



You're welcome. How have you acted upon it?



Here is my most updated version but i keep getting a textelement is not defined error in the console when the page runs:

 
john besty
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

john besty wrote:

Bear Bibeault wrote:

john besty wrote:Thanks for the feedback



You're welcome. How have you acted upon it?



[/code]



It all works after days of getting stuck!, I realised I had put the even listener outside the scope of the function and it couldn't connect but now saves all entries to local storage, it took completely starting again to fix it in seconds, thank you for your feedback though it helped.
 
Bear Bibeault
Marshal
Posts: 67510
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Glad you've got it working. Even so, I would take heed of the issues I addressed; as written, the code is tenuous and fragile.
 
john besty
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:Glad you've got it working. Even so, I would take heed of the issues I addressed; as written, the code is tenuous and fragile.


Oh don't I know it! 😂
 
Villains always have antidotes. They're funny that way. Here's an antidote disguised as a tiny ad:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic