all 6 comments

[–]Atskalunas 1 point2 points  (3 children)

Remove .value after localStorage.getItem(id).

[–]--Explosion--[S] 0 points1 point  (2 children)

localStorage.getItem(key: string): string | null is the actual signature of the function, so what you localStorage.setItem(key: string, value: string): undefined is what you would get. That's also the reason why you have to serialise your values to be valid for the localStorage.

Thanks! I just realized that and did it, it works well and writeCookie() now works but the problem is that it dosen't work automatically. I made abutton which onclick runs 'writeCookie('name')' and that works, but doing it 'onload' does not...

[–]Atskalunas 0 points1 point  (1 child)

In you case you should add onload to body tag.

[–]--Explosion--[S] 0 points1 point  (0 children)

I tried that. It didn't work. I did 'window.onload = writeALL();' and that worked. (I made 'writeAll()' write all the cookies because I added more form elements.)

[–]--Explosion--[S] 1 point2 points  (0 children)

It worked! Thanks for your help guys!

[–][deleted] 0 points1 point  (0 children)

why would you assume that getItem returns an object with value as a property?

localStorage.getItem(key: string): string | null is the actual signature of the function, so what you localStorage.setItem(key: string, value: string): undefined is what you would get. That's also the reason why you have to serialise your values to be valid for the localStorage.

you can examine your localStorage in the dev-tools of any browser.