• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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: 15484
363
  • 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?
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • 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! 😂
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello i am working on a similar project and would love to be helped by you guys. Please
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Daniel, welcome to the Ranch!

You're free to post your question here. Find the forum you'd like to post in, or do your best to find one, there's a lot of them. Then you should find a "New Topic" button. Click that and off you go.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi John would it be possible for you to post your solution?
 
William Finlay
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

john besty wrote:

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.




Hi John would it be possible for you to post your solution?
 
Any sufficiently advanced technology will be used as a cat toy. And this tiny ad contains a very small cat:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic