Discuss Scratch

god286
Scratcher
1000+ posts

How do I create a new element with a class with JS

MagicCrayon9342 wrote:

I have no idea on how localstorage works
You should learn it, it will help you in the future.
Basically you can store key-value pairs. The value has to be a string though.

Here are some of my followers!

I joined: 5 years, 9 months, 24 days ago (31/03/2018)
I have: 479 followers
In total, I have attained: 1,403 loves, 1,145 favourites, and 33,731 views.
Fun Fact: If my account continued to gain followers at a similar rate to right now, in 14,210 years I would reach the number of followers griffpatch has today! Try to imagine how many followers he would have then!
Thank you everyone!
Script created by god286.
MagicCrayon9342
Scratcher
1000+ posts

How do I create a new element with a class with JS

god286 wrote:

MagicCrayon9342 wrote:

I have no idea on how localstorage works
You should learn it, it will help you in the future.
Basically you can store key-value pairs. The value has to be a string though.
well considering what your telling me doesn't help at all I'll just have to go without the feature until I figure it out in like 20 years.

Actual code goes way farther and makes the flow of software creation much faster than links.

Last edited by MagicCrayon9342 (Jan. 16, 2022 01:03:08)


MagicCrayon9342
Scratcher
1000+ posts

How do I create a new element with a class with JS

also the code immediately breaks upon switching to tailwind so I won't be doing that this time.

MagicCrayon9342
Scratcher
1000+ posts

How do I create a new element with a class with JS

for now I'm making the buttons a bit nicer with :hover suffixes and some extra css

CST1229
Scratcher
1000+ posts

How do I create a new element with a class with JS

MagicCrayon9342 wrote:

umm well I have no idea on how localstorage works and nothing on stack overflow works
A quick guide:
// get local storage (you could use window.localStorage everytime but that's typing more characters)
const storage = window.localStorage;
 
// set values
storage.setItem("key", "value");
 
// NOTE: values are automatically converted to strings, I think using x.toString().
// for objects, this returns "[object Object]" and for arrays, it's a comma-separated list of values
// so you might want to JSON.stringify() the value before storing, then JSON.parse() it afterhand
 
// get values
storage.getItem("key") // -> value
// console.log(storage.getItem("key"))
 
// remove an item
storage.removeItem("key");
 
// clear this site's localstorage
storage.clear();
 
// there's also storage.length and storage.key(number)

Last edited by CST1229 (Jan. 16, 2022 03:19:39)


This is a signature. It's a piece of text that appears below every post I write. Click here to learn more, including how to make your own.
RIP assets image hosting. 2013?-2023



MagicCrayon9342
Scratcher
1000+ posts

How do I create a new element with a class with JS

CST1229 wrote:

MagicCrayon9342 wrote:

umm well I have no idea on how localstorage works and nothing on stack overflow works
A quick guide:
// get local storage (you could use window.localStorage everytime but that's typing more characters)
const storage = window.localStorage;
 
// set values
storage.setItem("key", "value");
 
// NOTE: values are automatically converted to strings, I think using x.toString().
// for objects, this returns "[object Object]" and for arrays, it's a comma-separated list of values
// so you might want to JSON.stringify() the value before storing, then JSON.parse() it afterhand
 
// get values
storage.getItem("key") // -> value
// console.log(storage.getItem("key"))
 
// remove an item
storage.removeItem("key");
 
// clear this site's localstorage
storage.clear();
 
// there's also storage.length and storage.key(number)
ok, also does anyone know why stack overflow code never works?

MagicCrayon9342
Scratcher
1000+ posts

How do I create a new element with a class with JS

Can I save the DOM to localStorage as a string then every time the app is loaded it loads the DOM back in to the page.
I'm going to have a button function

function save() {
  // code here

<button onClick="save()">Save</button>

CST1229
Scratcher
1000+ posts

How do I create a new element with a class with JS

MagicCrayon9342 wrote:

Can I save the DOM to localStorage as a string then every time the app is loaded it loads the DOM back in to the page.
I think you should store the cards in something like an array or an object then recreate them when loading but if you really want to store the DOM, you can get/set the innerHTML of the cards container and store that in localstorage.

Last edited by CST1229 (Jan. 16, 2022 04:07:04)


This is a signature. It's a piece of text that appears below every post I write. Click here to learn more, including how to make your own.
RIP assets image hosting. 2013?-2023



NFlex23
Scratcher
1000+ posts

How do I create a new element with a class with JS

MagicCrayon9342 wrote:

(#26)

CST1229 wrote:

MagicCrayon9342 wrote:

umm well I have no idea on how localstorage works and nothing on stack overflow works
A quick guide:
// get local storage (you could use window.localStorage everytime but that's typing more characters)
const storage = window.localStorage;
 
// set values
storage.setItem("key", "value");
 
// NOTE: values are automatically converted to strings, I think using x.toString().
// for objects, this returns "[object Object]" and for arrays, it's a comma-separated list of values
// so you might want to JSON.stringify() the value before storing, then JSON.parse() it afterhand
 
// get values
storage.getItem("key") // -> value
// console.log(storage.getItem("key"))
 
// remove an item
storage.removeItem("key");
 
// clear this site's localstorage
storage.clear();
 
// there's also storage.length and storage.key(number)
ok, also does anyone know why stack overflow code never works?
Which “stack overflow code” doesn't work?

Here are some SO links that work and should answer your questions:

Last edited by NFlex23 (Jan. 16, 2022 11:50:58)


Help improve the Advanced Topics (Really!)
Before you create a topic:
Always search for duplicates or other similar topics before making an umbrella topic, e.g., “The Mac Topic”.
  • Is it about something you are planning on making but haven't made yet? If so, please wait to post until you have created a working prototype. This is a key factor to keeping the ATs as clean as possible.
  • The ATs aren't technical support. It is perfectly valid to ask questions about things related to programming, but not issues with external websites, apps, or devices. Most sites have their own support system; try asking there!
  • Is it related to something you are making in Scratch? (This includes OSes and other Scratch projects) If so, please post in Collaboration, Show and Tell, or another similar forum.
  • Is your topic questionably “advanced”? Try browsing the other forums to see if your topic fits better in one of those.
  • Issues with Scratch itself should be put in Bugs and Glitches.
Before you post: Is what you're posting likely to start an argument or derail the thread (e.g., browsers, operating systems)? If so, please re-think your post!





Maximouse
Scratcher
1000+ posts

How do I create a new element with a class with JS

MagicCrayon9342 wrote:

ok, also does anyone know why stack overflow code never works?
It almost always works if you change things like variable names to match your existing code.


This is Maximouse's signature. Learn more about signatures.
MagicCrayon9342
Scratcher
1000+ posts

How do I create a new element with a class with JS

Maximouse wrote:

(#30)

MagicCrayon9342 wrote:

ok, also does anyone know why stack overflow code never works?
It almost always works if you change things like variable names to match your existing code.
most of the time the existing code is from stack overflow

god286
Scratcher
1000+ posts

How do I create a new element with a class with JS

MagicCrayon9342 wrote:

most of the time the existing code is from stack overflow
stackoverflow isnt a person its a community

Here are some of my followers!

I joined: 5 years, 9 months, 24 days ago (31/03/2018)
I have: 479 followers
In total, I have attained: 1,403 loves, 1,145 favourites, and 33,731 views.
Fun Fact: If my account continued to gain followers at a similar rate to right now, in 14,210 years I would reach the number of followers griffpatch has today! Try to imagine how many followers he would have then!
Thank you everyone!
Script created by god286.

Powered by DjangoBB