Increase by 2 when activating function

0

Hello, I am a beginner in javascript and would like to know how to block the item, when var total reaches that value equal to that of the item, ex: 200, increase by 2 not 2 by 1.

var total = 1;
function clickAumento () {
  document.getElementById('marioClick').value = total++;

}
<div id="divContador">
    <input type="text" id="marioClick" value="0"></input>
</div>
<div id="divMario"> 
   <img onclick="clickAumento()" id="mario" src="http://www.imagenspng.com.br/wp-content/uploads/2015/02/super-mario-mario-11.png">
</div>
    
asked by anonymous 16.05.2018 / 16:14

2 answers

2

As for the +2 increase I suggest:

function clickAumento () {
  document.getElementById('marioClick').value = total += 2;
}
    
16.05.2018 / 17:12
0

There are many ways to do this. One of them is using the removeAttribute method to remove onclick when it reaches the desired number.

For 2 in 2 increments, change total++ by total+=2 .

In the example below, when the number reaches 20 (for example only), the onclick attribute is removed and there will be no further action when the image is clicked:

var total = 1;
function clickAumento () {
//   if(total < 200){
   if(total < 20){
  document.getElementById('marioClick').value = total+=2;
   }else{
      document.getElementById("mario").removeAttribute("onclick");
   }

}
<div id="divContador">
    <input type="text" id="marioClick" value="0"></input>
</div>
<div id="divMario"> 
   <img height="300" onclick="clickAumento()" id="mario" src="http://www.imagenspng.com.br/wp-content/uploads/2015/02/super-mario-mario-11.png">
</div>
    
17.05.2018 / 03:48