i want to make when that on click will replace to “count is odd!” or “count is even”
but how i should declare count so it won’t reinitialized to 1 each click event?
here is what i came with:
<h1 onclick="var count=1;
if (count%2==1) {
this.innerHTML='count is odd!'; count++;}
else{
this.innerHTML='count is even';count++;} ">
Click on this text</h1>
now what happens as you can see is that count is one on each click.
You can try this, the
countervariable is inonclickclosure scope and thus it will act like a global variable for allonclickevents.NOTE: You don’t need
onclickinline code your elements.