Increase by 2 when activating Function

Asked

Viewed 431 times

0

Hello, I’m a beginner in javascript and I would like to know how to block the item, when var total reaches the same value as the item, ex: 200, increase 2 by 2 not 1 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>

  • tried to use total+2?

  • 1

    John your question is confused, not to understand. It says: how do I lock the item when unlock on click, could edit and better clarify your problem or difficulty ?

  • What do you mean, "block"?

  • I edited!! basically fundamentals of a Clicker game

  • block the click?

2 answers

3

With regard to the +2 increment I suggest:

function clickAumento () {
  document.getElementById('marioClick').value = total += 2;
}

1

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

With regard to the 2-by-2 increment, change the total++ for total+=2.

In the example below, when the number reaches 20 (for example only), the attribute onclick is removed and there will be no more 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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.