How to increase value of Variable on every click ??? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 6

How to increase value of Variable on every click ???

need function

20th Jun 2017, 10:24 AM
Kailash Loncha
Kailash Loncha - avatar
4 Answers
+ 8
If your variable is in the global scope (or accessible through it), simply put "myvar++;" in the onclick event attribute of the targeted html element (tag). If the variable is in a particular scope (for example an anonymized function), you must attach the 'click' event with the .addEventListener() JS method of the targeted html element: window.onload = function() { var myVar = 0; function myEventHandler() { myVar++; // or: myVar += value_to_add; } document.getElementById('idOfElement').addEventListener('click',myEventHandler); };
20th Jun 2017, 10:45 AM
visph
visph - avatar
+ 4
Can you please post a link to your code context, so we can be accurate ;) Anyway, I allready have posted a code example... Just add correct id to the html element you want react to click: <div id="idOfElement">click me to increase variable value</div>
20th Jun 2017, 11:05 AM
visph
visph - avatar
+ 2
can you please make a code ???
20th Jun 2017, 10:57 AM
Kailash Loncha
Kailash Loncha - avatar
+ 2
Thanks
20th Jun 2017, 12:36 PM
Kailash Loncha
Kailash Loncha - avatar