Change text with Javascript

-2

Hello,

Can anyone help me with a JS script to change the button text instantly?

But change only when the status of the $ x variable changes from 0 to 1, or when you click on the button that contains the text.

    
asked by anonymous 29.03.2018 / 04:04

1 answer

0

The button label can be changed by clicking, like this:

<button class="meu-botao">Clique aqui</button>

<script>
    (function($){
        var $botao = $('.meu-botao');

        //Ao clicar no botao
        $botao.on('click', function(e) { 
            $botao.html("Botão clicado");
        });
    })(jQuery);
</script>

Changing the button label when a variable changes value can be done with Proxy , but it is important that this variable is part of an object

<button class="meu-botao">Clique aqui</button>

<script>
    (function($){
        var $botao = $('.meu-botao');
        var $y = {valor: 0};
        var checarAlteracao = {
            set(obj, prop, valor) {
                var label = (valor > 0)? "Variável alterada": "Clique aqui";
                $botao.html(label);
                return Reflect.set(...arguments);
            }
        };

        //Altera no duplo clique do botão
        $botao.on('dblclick', function(e) {
            $x.valor++;
        });

        var $x = new Proxy($y, checarAlteracao);
    })(jQuery);
</script>
    
29.03.2018 / 06:14