0
nine
3y

I have a button using
localStorage.setItem(‘button’, getElementById(‘buttonId’))

And use it on page reload for clicking the button on page reload
localStorage.getItem(‘button’).click()

What’s wrong here?

Comments
  • 1
    I never thought of taking the element and storing it in local storage, pretty sure you can't do that ... well and use it. I find that idea fairly amusing.

    For the record folks on this site sometimes don't respond well to technical how tos / troubleshooting. Just one of those things.
  • 2
    No, you can only store strings there, and why do you need to store DOM object there, you're doing something wrong.
  • 1
    Local storage can only store strings, so storing a DOM element would cause it to lose any attributes it had. This is also super bad practice, try storing the ID of the element, that way you can access the element by pulling the ID from localStorage, selecting by getElementById(), and then calling click
  • 1
    @theabbie lol didn’t knew that. What I am trying to do is if I click a specific button, I want a it to be active even on page reload. Can you suggest something?
  • 4
    @nine Store that button ID, which will be a string, also, if you don't want to permanently store it, you can store it in window.name variable, it persists on reload.
  • 2
    But what do you mean by active?

    A button has no active state as such unless you have built it.

    So what your trying to do is just going to click the button on every reload.

    Even if it works I would say its an antipattern.

    You should instead have an onload that directly sets any state the button sets.

    Because even if you right now have not on click action except setting the state its quite possible some one later adds one not expecting it to be triggered over and over.
  • 1
    @theabbie you are saviour
  • 1
    @theabbie Ok, the window.name persistence is something new for me.
  • 0
    I thought this was a quiz not an actual question lol
  • 0
    In jquery you can just do $(‘#buttonid’).click()

    Didn’t know there was something called local storage in js
  • 0
    @MadMadMadMrMim Is that a joke?
  • 0
    I have 14gb large zip file. Want to post these data using spring rest template.
    Am getting out of memory exception when file size is more than 2 gb.
    Any lead much appreciated.
  • 0
    @c3r38r170 nope I never use it lol I use this thing called session or a database lol
  • 0
  • 0
    @pankaj211 nice that you want to hijack a post with a random question

    Don't read such a file in one go. Stream the data within acceptable limits
  • 0
    @MadMadMadMrMim Different tools for different tasks. Using some more HTTP requests is overkill for some simple configurations.
  • 0
    @MadMadMadMrMim And I was talking about the jQuery tho'... You not only didn't answer the question correctly, but used jQuery for something that doesn't need it.
  • 0
    @c3r38r170 eh i used jquery in a way that someone would use jquery who uses jquery :P ok though document.getElementById("myid").click() :P
  • 1
    so simple and up front in the localstorage docs it is storing key'd data like a dictionary... kind of like a session, if you retrieve data from there its not likely getting parsed into an object, most especially a dom one however let me try something

    document.getElementById('comment')

    ouput: <textarea type=​"text" class=​"comment-text-input" placeholder=​"Add your 2 cents..." name=​"comment" id=​"comment">​</textarea>​

    localStorage.setItem('test',document.getElementById('comment'))

    localStorage.getItem('test')
    output: "[object HTMLTextAreaElement]"

    nope doesnt work because its not doing a JSON stringify and then a parse, its just dumping a toString()

    better luck next time

    use the developer console.
  • 1
    @MadMadMadMrMim That's muuuch better.
  • 0
    @c3r38r170 wheres my plus one then :P I need the reinforcing giddiness that comes from an entirely artificial system of reward which represents the esteem of my peers goddamn it !!! FEED MY DAMN NEEDS ! LOL
  • 0
    @c3r38r170 you did get the joke right ? :P kind of a rip on all social media really lol its what we replaced actual social interaction with since the unwashed masses are now all braindead robots that dont know how to have fun of any kind anymore and think everything represents something else apparently which is half the reason i keep blocking this dumb crap out
Add Comment