How to assign the value of a closure variable to a global variable. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

How to assign the value of a closure variable to a global variable.

var x=""; // here is the closure (function (index){ //click handler on array of elements to retrieve //their index ... [i].onclick=function (){ /*What happens here? can we assign the index variable here to a global one like x and later do stuff with x */ return x } })(i);

3rd Aug 2017, 5:13 PM
Abdoulaye Seck
Abdoulaye Seck - avatar
2 Answers
+ 4
basically I have 4 images on a main page. I want onclick to open the target link and retrieve the clicked image src and append it to a div on the target page.
8th Aug 2017, 3:58 PM
Abdoulaye Seck
Abdoulaye Seck - avatar
+ 1
I'm not sure to understand what you want to achieve, but you could probably attach it to the window like "window.x = value" to be sure to retrieve it later.
5th Aug 2017, 1:38 PM
Hepson
Hepson - avatar