chickenMan
chickenMan

Reputation: 11

how to use jquery to save the text on the page even after refreshing it?

  $(".saveBtn").click(function() {
  value = $(this).siblings("textarea").val();
  hourString = $(this).siblings("div").text();
  localStorage.setItem(hourString, JSON.stringify(value));

'''I think I have it saved to local storage, now how to i get it to display'''

Upvotes: 1

Views: 69

Answers (1)

Torsten Barthel
Torsten Barthel

Reputation: 3458

You can use LocalStorage like so:

window.localStorage.setItem('key', 'content')

And read it like so:

const val = window.localStorage.getItem('key')

In your case you will save it that way:

$(".saveBtn").click(function() {    
    const value = $(this).siblings("textarea").val();
    localStorage.setItem('myVal', value);
})

So you can display it by the textarea with following

const savedVal = localStorage.getItem('myVal')
$(this).siblings("textarea").val(savedVal);

Upvotes: 1

Related Questions