Change button value without showing alert

Asked

Viewed 54 times

-2

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

Follows code:

<button  onclick="alert(this.value);this.value -= 1;" value="200">200</button>
  • What is 200 ? Try to explain better.

  • Please elaborate your question and if possible enter the code you are using.

  • Do not put in the comment. Ask the question.

  • I can’t put in the question!

  • @Jdchaves, You want to decrease the alert button ?

  • No I want to remove the alert and decrease the values without the alert appears!

  • @Jdchaves, You want to change the value without showing alert ?

  • That’s right Matheus!

  • @Jdchaves looks at my answer.

  • Okay I’ll check here!

Show 5 more comments

2 answers

2


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

Already to update the text button (which is not the same as the attribute value), you should also update the property 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 attribute value="200", as long as the innerHTML updates the button text. In the examples above I’m assuming you want to change both (because that’s what makes most sense to me).

  • 1

    Bro thanks! That’s what I wanted! Tmj was worth❤

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>
<button id="id_do_botao" value="200">200</button>

  • Matheus like, I want the value to decrease by clicking on the right button? So more I don’t want to keep popping this Alert I wanted the value decrease on top of the same button or down tended?

  • @Jdchaves my code is doing what ? Click run and test.

  • rsrsrs.. I guess he didn’t go with your guy.

  • I tested bro but it didn’t work!

  • 1

    @Leandrade because it is kkk

  • 2

    @Matheusmiranda I think you did with jQuery, then it didn’t work there for him.

  • Kkkk not bro none of this sorry bro I tested more didn’t work!

  • Thanks for the help Matheus!!❤

Show 3 more comments

Browser other questions tagged

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