Change button value without showing alert [closed]

-2

I want to change the value of the button always with -1, without displaying alert.

Follow the code below:

<button  onclick="alert(this.value);this.value -= 1;" value="200">200</button>
    
asked by anonymous 26.07.2018 / 20:20

2 answers

2

To prevent the alert from appearing, simply remove the call from alert() .

Now to update the text button (which is not the same as the value attribute), you should also update the innerHTML :

function diminuiValor(botao) {
    // diminui o "value"
    botao.value -= 1;
    // atualiza o texto do botão para ser o mesmo que o value
    botao.innerHTML = botao.value;
}
<button onclick="diminuiValor(this);" value="200">200</button>

Or, more directly (without creating a function ):

<button onclick="this.value -= 1; this.innerHTML = this.value;" value="200">200</button>

In short, this.value -= 1 decreases the value that was set in the value="200" attribute, while innerHTML refreshes the button text. In the above examples I'm assuming you want to change both (because that's what makes the most sense to me).

    
26.07.2018 / 20:50
0

See the example you want:

$("#id_do_botao").click(function() {
  var valor = this.value -= 1;
  $(this).text(valor);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><buttonid="id_do_botao" value="200">200</button>
    
26.07.2018 / 20:35