Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript set local storage value

var testObject = { 'one': 1, 'two': 2, 'three': 3 };

// Put the object into storage
localStorage.setItem('testObject', JSON.stringify(testObject));

// Retrieve the object from storage
var retrievedObject = localStorage.getItem('testObject');

console.log('retrievedObject: ', JSON.parse(retrievedObject));
Comment

javascript set localstorage

// SET
localStorage.setItem('myCat', 'Link');

// GET
let aValue = localStorage.getItem('myCat');
Comment

set localstorage

function set(){
    var sendJson = JSON.stringify(arrayName);
    localStorage.setItem('key',sendJson)
}
Comment

set localstorage value

localStorage.setItem("mytime", Date.now());
Comment

get and set data in local storage javascript

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Save Card data on local storage</title>
</head>

<body>
    <input type="text" id="product_name" placeholder="Product Name">
    <input type="text" id="product_quantity" placeholder="Product Quantity">
    <button id="btn_card">Add to Card</button>



    <section>
        <ul id="display-Field">

        </ul>
    </section>


    
    <script>
        // 1 get input value from html
const getInputValueById = (id) => {
    const inputField = document.getElementById(id);
    const inputValue = inputField.value;
    inputField.value = '';
    return inputValue;
};


// 2 set click event on btn and start operation
document.getElementById('btn_card').addEventListener('click', () => {
    const productName = getInputValueById('product_name');
    const productQuantity = getInputValueById('product_quantity');
    setValueOnLocalStorage(productName, productQuantity);
    addDisplayProduct(productName, productQuantity);

});


// 3 check isCart ablavail in localstorage 
const getCardFromLocalStorage = () => {
    const savedCart = localStorage.getItem('cart');
    let cart = {};
    if (savedCart) {
        cart = JSON.parse(savedCart);
    }
    return cart;
};


//4 add value in to the object and set to the local storage 
const setValueOnLocalStorage = (product, quantity) => {
    const cart = getCardFromLocalStorage();

    // set value in object 
    cart[product] = quantity;
    const cartStringified = JSON.stringify(cart);


    // save to the local storage 
    localStorage.setItem('cart', cartStringified);
};


//5 show product on display
const addDisplayProduct = (productName, quantityNum) => {
    const displayField = document.getElementById('display-Field');
    const porductItem = document.createElement('li');
    porductItem.innerHTML = `
    ${productName} - ${quantityNum}
    `;
    displayField.appendChild(porductItem);
};


// 6 get product list from local storage 
const showProduct = () => {
    const cart = getCardFromLocalStorage();
    for (let product in cart) {
        addDisplayProduct(product, cart[product]);
    }
};


// 7 call function-6
showProduct();

    </script>
</body>

</html>
Comment

PREVIOUS NEXT
Code Example
Javascript :: check ObjectId is valid in mongoose 
Javascript :: format money javascript commas 
Javascript :: android center text react native 
Javascript :: git ignore .env files not working 
Javascript :: invalid host header vue 
Javascript :: get children length jquery 
Javascript :: javascript check if element has class 
Javascript :: jquery trigger change event 
Javascript :: jest wait for x seconds 
Javascript :: react install 
Javascript :: update core-js 
Javascript :: set value javascript by id 
Javascript :: javascript math.random from list 
Javascript :: javascript disable input 
Javascript :: javascript function to format phone number 
Javascript :: js random number between 1 and 100 
Javascript :: .gitignore nodejs 
Javascript :: warning ../../../package.json no license field 
Javascript :: await async sleep 
Javascript :: fetch get authorization header 
Javascript :: javascript biggest number 
Javascript :: how to change background image using javascript 
Javascript :: regex for numbers and decimals only 
Javascript :: add button in table using javascript 
Javascript :: required false jquery 
Javascript :: jquery show for 5 seconds 
Javascript :: cypress clear session storage 
Javascript :: set width of jqgrid 
Javascript :: jquery change font size 
Javascript :: how to get the first character of a string in javascript 
ADD CONTENT
Topic
Content
Source link
Name
9+5 =