+ 6
How to increase value of Variable on every click ???
need function
4 Antworten
+ 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);
};
+ 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>
+ 2
can you please make a code ???
+ 2
Thanks