i was wondering how i can increment for example 1 when i click on a button and decrement 1 when i click the second time on this button on the same function.
If someone can explain me ! Thank you
CodePudding user response:
Need some more information. But this seems like an interesting function I just wrote down
function foo() {
if (foo.value == null) foo.value = 0;
if (foo.value >= 1) return --foo.value;
if (foo.value <= 0) return foo.value;
return foo.value;
}
CodePudding user response:
quick example (not tested):
<div onclick="clickfunction()">...</div>
<style>
div[is_selected] { background-color:red; }
</style>
<script>
var counter=0;
function clickfunction(e)
{
if(counter>=5) return;
if(e.target.getAttribute('is_selected'))
{
counter--;
e.target.removeAttribute('is_selected');
return;
}
counter ;
e.target.setAttribute('is_selected', 1);
}