Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

local

const form = document.querySelector('form')
const ul = document.querySelector('ul')
const button = document.querySelector('button')
const input = document.getElementById('item')
let itemsArray = localStorage.getItem('items')
  ? JSON.parse(localStorage.getItem('items'))
  : []

localStorage.setItem('items', JSON.stringify(itemsArray))
const data = JSON.parse(localStorage.getItem('items'))

const liMaker = (text) => {
  const li = document.createElement('li')
  li.textContent = text
  ul.appendChild(li)
}

form.addEventListener('submit', function (e) {
  e.preventDefault()

  itemsArray.push(input.value)
  localStorage.setItem('items', JSON.stringify(itemsArray))
  liMaker(input.value)
  input.value = ''
})

data.forEach((item) => {
  liMaker(item)
})

button.addEventListener('click', function () {
  localStorage.clear()
  while (ul.firstChild) {
    ul.removeChild(ul.firstChild)
  }
})
Comment

PREVIOUS NEXT
Code Example
Javascript :: passing ref to child component 
Javascript :: axios async await 
Javascript :: add property with value in js 
Javascript :: what is node 
Javascript :: javascript prototype inheritance 
Javascript :: materialze 
Javascript :: .default in javascript 
Javascript :: use of slot in vue 
Javascript :: class component params in react 
Javascript :: rimraf node.js 
Javascript :: controlled string variable npm script run 
Javascript :: javascript block link action 
Javascript :: angular dinamic disabled 
Javascript :: node.js server-side javascript 
Javascript :: javascript nested objects 
Javascript :: recoilOutside npm 
Javascript :: show mwssage js 
Javascript :: mongoose query same field with different values 
Javascript :: gdscript create node 
Javascript :: python to javascript converter online 
Javascript :: how to update a function to accept a name and have it displayed in JavaScript 
Javascript :: how to enter js in html 
Javascript :: package.json laravel best 
Javascript :: Validate a gregorian date 
Javascript :: react state scope 
Javascript :: redux store as number instead of string 
Javascript :: javascript check alpha and space only 
Javascript :: curly j 
Javascript :: angry professor javascript 
Javascript :: backbone js event listener 
ADD CONTENT
Topic
Content
Source link
Name
7+8 =