Local Storage

Discussion in 'JavaScript' started by Sam085789, Mar 6, 2022.

  1. #1
    Good Afternoon all,

    I am currently learning JavaScript, My task is to remove items from localStorage.
    The set keys are timestamps for example
    
    // Make a key using a fixed timestamp
        key = "diary" + "1536771000001";.
    
    // Make a key using a fixed timestamp
        key = "diary" + "1536771000002"; (ETC)
    
    Code (JavaScript):
    However, when i go to remove these items using localStorage.removeItem('diary1536771000002'); every time i click the delete button on any item it removes the diary1536771000002 all the time and not the right key for that text item.

    I am not looking for the direct answer but can someone point me in the right direction?
     
    Last edited by a moderator: Mar 6, 2022
    Sam085789, Mar 6, 2022 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,821
    Likes Received:
    4,539
    Best Answers:
    123
    Trophy Points:
    665
    #2
    I suspect that you have the delete buttons calling the same function but the function has the key hardcoded.

    An alternative might be to have buttons with data-key='diary1536771000002' and then the click event picks up the dataset.key value.
     
    sarahk, Mar 6, 2022 IP