jQuery | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

jQuery

How can I use the value of the 'a' variable outside the click function? $("btn").click(function(){ var a = 0; a++; }); now i want use value of 'a' outside of function. Please help..

7th Dec 2017, 2:06 AM
Amir Mehr
Amir Mehr - avatar
3 Answers
+ 12
In most programmimg languages the variable scope exists or visible within the braces {} only. Therefore if you want to access it from the scope outisde, then you can DECLARE it outside as well and ASSIGN it within the scope. 😉
7th Dec 2017, 3:57 AM
Zephyr Koo
Zephyr Koo - avatar
+ 4
Hi, u need to define ur variable where u need, and than u must define ur function like: function(int a_) { car a_=0; a_++; } And than u can use ur function: function(a); Hope it's help!
7th Dec 2017, 2:20 AM
stKhaDgar
stKhaDgar - avatar
+ 2
Just declare variable outside and change inside. You're done. var a $("btn").click(function(){ a=0 a++ }) _________________________ or in ES6 style let a $('btn').click( () => { a=0 a++ } //any other actions with a anywhere you want
7th Dec 2017, 9:12 PM
Rose Sevenyears
Rose  Sevenyears - avatar