0

I have this function:

    const btns = document.querySelectorAll('#btn')
    const items = document.querySelector('#item')
    btns.forEach(btn => {
        btn.addEventListener('click', (e) =>{
                const item = e.currentTarget.parentElement
                item.classList.toggle('strike')
        })
    })

Now, how can I save classList.toggle('strike') to localStorage?

I tried like this:

window.addEventListener('DOMContentLoaded',() => {
    const listItems = items.map(item => {
        return `
            <ul>
                <li id='item'>${item}</li>
                <button id='btn'>Click</button>
           </ul>
        `
    })
    content.innerHTML = listItems.join(' ')
    const btns = document.querySelectorAll('#btn')

    btns.forEach(btn => {
        btn.addEventListener('click', (e) =>{
            if(btn) {
                const item = e.currentTarget.parentElement
                const wasToggled = localStorage.getItem('strike') === 'true'
                localStorage.setItem('strike', !wasToggled)
                item.classList.toggle('strike', !wasToggled)
            }
        })
    })
    window.onload = () => {
        items.classList.toggle('strike', localStorage.getItem('strike') == 'true')
    }

However, it was kinda slow and there are some things I don't understand from this code that I found somewhere:

Why has the true be a string rather than a boolean? When I type true as a boolean rather than a string why it doesn't work is there any difference between true and 'true'? When the page is refreshed it saves the classList to the first item in the list, why is that?

Sorry, I'm a noob...

Barthy
  • 3,151
  • 1
  • 25
  • 42
Anonymous
  • 11
  • 3
  • The 'true' is a string because it comes from localStorage, and everything in localStorage is stored as strings, also you seems to have multiple buttons but only one item in localStorage, with a 'true'/'false' value, which will not work, because you can't know which button was clicked. Instead of 'true'/'false' you will need to store the button id that was clicked, or an array of ids if there is multiples at the same time – Lk77 Nov 26 '22 at 11:09
  • Yes but I use `array.map()` method so there is more than 1 button. Do I need the array as an object with IDs? So how can I add an id to each button or array, so that I will save to localStorage? – Anonymous Nov 26 '22 at 11:25
  • The localStorage is shared across your page, so your need to give an id to each button, and store the state in the strike item. You could have an object like : `{idBtn1: false, idBtn2: true}`. Each time a button is clicked, retrieve the item, and change the boolean for the given button, and store it back in the localStorage – Lk77 Nov 26 '22 at 14:38
  • `document.querySelectorAll('#btn')` doesn’t make sense. Duplicate IDs are invalid. [Validate your HTML](//validator.nu). Use [event delegation](//developer.mozilla.org/en/docs/Learn/JavaScript/Building_blocks/Events#Event_delegation) instead of adding several event listeners — it’s more maintainable and applies to dynamically added elements. See [the tag info](/tags/event-delegation/info) and [this Q&A](/a/55452921/4642212). Consider serializing Local Storage entries as [`JSON`](//developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON). – Sebastian Simon Nov 26 '22 at 15:25

0 Answers0