Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript and the DOM (Retiring) Making Changes to the DOM Appending Nodes

Dante Ortiz
Dante Ortiz
3,844 Points

How can I keep the newly created element in the page after refreshing?

I want to create a button using another button and keep it after refreshing the page.

1 Answer

For this you will have to store the data somewhere, there are a bunch of different options, you should check them out and see which fits your need best.

  • Sessionstorage - Allows you to access a storage object that lasts for that current session.
  • Local storage - Similar to Session storage, it allows you to access a storage object but local storage has no expiration.
  • Cookies - Send data from the server to the browser then vise versa to store the users data.

There are others but these are the ones that i think your looking for

Not sure what your situation is but heres a simple example with local storage:

// Do something to set the localStorage, when you create that element
button.addEventListener( 'click', function() {
   localStorage.setItem( key, value );
});

// Check the localStorage then create the element
if ( localStorage.getItem( key ) === value ) {
    // Create the element you stored
}