Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting and getting localStorage with jQuery

I am trying out localStorage and attempting at getting text from a div and storing it in localStorage, however, it sets it as an [object Object] and returns [object Object]. Why is this happening?

localStorage.content = $('#test').html('Test');    $('#test').html(localStorage.content);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>    <div id="test"></div>
like image 841
werewr Avatar asked Nov 24 '16 16:11

werewr


People also ask

How jquery push data in local storage?

Use setItem and getItem if you want to write simple strings to localStorage. Also you should be using text() if it's the text you're after as you say, else you will get the full HTML as a string.

What is the code syntax to set localStorage value?

setItem() : How to store values in localStorage window. localStorage. setItem('name', 'Obaseki Nosa'); Where name is the key and Obaseki Nosa is the value.

How to store a jQuery object in localStorage?

:-) I upvoted and appreciate your good intention. The localStorage can only store string content and you are trying to store a jQuery object since html (htmlString) returns a jQuery object. You need to set the string content instead of an object. And use the setItem method to add data and getItem to get data.

How to get/set localStorage keys from another window?

The localStorage is shared between all windows with the same origin, so if we set the data in one window, the change becomes visible in another one. We can also use a plain object way of getting/setting keys, like this: localStorage. test = 2; alert( localStorage. test ); delete localStorage. test;

What is localStorage and how to use it?

The main features of localStorage are: Shared between all tabs and windows from the same origin. The data does not expire. It remains after the browser restart and even OS reboot. For instance, if you run this code… …And close/open the browser or just open the same page in a different window, then you can get it like this:

How to manage local storage and cookies simultaneously using jQuery?

Include keyboard shortcut from bash like CTRL+A, CTRL+D, CTRL+E etc. A jQuery plugin to manage local storage and cookies simultaneously in a simple interface. We found that HTML5 Local Storage gave us the best return in terms of reliability and speed.


1 Answers

You said you are attempting to get the text from a div and store it on local storage.

Please Note: Text and Html are different. In the question you mentioned text. html() will return Html content like <a>example</a>. if you want to get Text content then you have to use text() instead of html() then the result will be example instead of <a>example<a>. Anyway, I am using your terminology let it be Text.

Step 1: get the text from div.

what you did is not get the text from div but set the text to a div.

$('#test').html("Test");  

is actually setting text to div and the output will be a jQuery object. That is why it sets it as [object Object].

To get the text you have to write like this
$('#test').html();

This will return a string not an object so the result will be Test in your case.

Step 2: set it to local storage.

Your approach is correct and you can write it as

localStorage.key=value 

But the preferred approach is

localStorage.setItem(key,value); to set

localStorage.getItem(key); to get.

key and value must be strings.

so in your context code will become

$('#test').html("Test"); localStorage.content = $('#test').html(); $('#test').html(localStorage.content); 

But I don't find any meaning in your code. Because you want to get the text from div and store it on local storage. And again you are reading the same from local storage and set to div. just like a=10; b=a; a=b;

If you are facing any other problems please update your question accordingly.

like image 114
Tintu C Raju Avatar answered Sep 23 '22 07:09

Tintu C Raju