+ 1

How can I save variable's value in a function that restarts everytime I click on an html element?

For example, change div's background-color to a sequence of colors previusly specified, when the variable that i want to save is the index of the color's array (Sorry for my bad english)

13th Jan 2019, 5:39 AM
Luis Méndez
Luis Méndez - avatar
7 ответов
+ 1
Make 'change' as global variable,
15th Jan 2019, 5:24 AM
Reyot Mhz
Reyot Mhz - avatar
+ 7
Aditya Khandelwal your all answers seems to be directly copy pasting from other sites or external resource. If you want to help then post the links or your own answer Read the guideline before posting in the forum. Like your this answer is came from directly from this site. https://stackoverflow.com/questions/16206322/how-to-get-js-variable-to-retain-value-after-page-refresh https://www.sololearn.com/Content-Creation-Guidelines/
13th Jan 2019, 7:12 AM
GAWEN STEASY
GAWEN STEASY - avatar
+ 2
Luis Méndez I have some questions to help me better understand your question, so that I can provide a proper response. My understanding is you have a function that gets called during a click event. This function also declares some local variables with values you'd like to carry over between subsequent clicks. However, the variables are reset every time the function is called. Is this correct? It sounds like this would be a good use case for closures. However, I want to confirm this is what you are asking.
13th Jan 2019, 7:56 AM
David Carroll
David Carroll - avatar
+ 1
David Carroll yes, i want to save that variables
13th Jan 2019, 4:33 PM
Luis Méndez
Luis Méndez - avatar
0
Luis Méndez Add some code to Code Playground and show me what you have so far. It will be easier to work through the solution with an actual example of your issue. Post a link here for us to review.
13th Jan 2019, 5:07 PM
David Carroll
David Carroll - avatar
13th Jan 2019, 5:42 PM
Luis Méndez
Luis Méndez - avatar
0
It works! Thanks!! Daniel Sabrel
15th Jan 2019, 5:27 AM
Luis Méndez
Luis Méndez - avatar