Giter Site home page Giter Site logo

localstorage's Introduction

Web Storage

Web storage is data stored locally in a user's browser.

  • Local storage: data with no expiration date that will persist after the browser window is closed.
  • Session storage - data that gets cleared after the browser window is closed
  • cookies - ?

localStorage methods

Method Description
setItem() Add key and value to local storage
getItem() Retrieve a value by the key
removeItem() Remove an item by key
clear() Clear all storage

localStorage // Storage {length: 0}

Add Data to local Storage

Add data to localStorage by using the setItem() method. localStorage.setItem('key', 'value')

localStorage.setItem('name', 'ginger'); localStorage // {name: "ginger", length: 1}

Retrieve Data from local Storage

Retrieve the value of a particular key from local storage by using the getItem() method.

localStorage.getItem('name'); "ginger"

Remove Data from local Storage

localStorage.removeItem('name'); localStorage.getItem('name'); null

Clear Local Storage

localStorage.clear();

textContent

ele.textContent value is meant to be text only, not HTML vs ele.innerHTML value can include tags

Add ele.textContent to localStorage

  • the input value should be added to the localStorage as well as appear on the unordered list
  • also want to loop through all the existing local storage items and display them at the top of the list
  • Last, we want the "Clear All" button to remove all items from local storage
  1. Create an empty array to start
  2. Create a localStorage key called "items" with the array as a value
    • localStorage only supports strings as values
    • Convert a data array to a string by JSON.stringify(array)
    • To convert the contents of localStorage back into a data structure use JSON.parse();
  3. push any new input value into the array
  4. set the localStorage to the new, updated value

localstorage's People

Stargazers

Roman avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.