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>
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.
setItem() : How to store values in localStorage window. localStorage. setItem('name', 'Obaseki Nosa'); Where name is the key and Obaseki Nosa is the value.
:-) 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.
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;
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:
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With