Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to store object in a cookie so I can use it for further sessions?

Hi,

I have html like this:

<div id="inputarea" style="color:black;font-size:15px;">

I want to store the style data into a cookie so I did this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

setCookie('savedstyle',inputarea.style);

So in my next session inputarea should load the data from that cookie and set it as the style for the inputarea like this:

if(getCookie('savedstyle')) {
  inputarea.style = getCookie('savedstyle');
 }

nothing happens because the value stored in the cookie looks something like this: [object CSS properties]. Why is that? How can I store the value properly?

Thank you.

>Solution :

The .style in JS just stores setters for CSS attributes. It’s not useful for reading information.

If you just want to store the inline style use:

setCookie('savedstyle', inputarea.getAttribute('style'))

and to retrieve

inputarea.setAttribute('style', getCookie('savedstyle'))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading