modify variable shown in HTML from JS

0

Hello people, as you can see on the page, there is a button, in which I click and increase the variable credit that is shown in the console, but what I want is that this variable also increases where the number is 1 that is on the page next to credits, since the variable is not updated, this is the script:

<script>
    var credit = 0;
    function addCredit(){
            credit = credit + 1;
    }

    addCredit();

    document.write("Creditos ", credit);
</script>

And the button:

<button onclick="addCredit()">Free Credits</button>

It would be a great help who would answer me about the problem, I am new to programming.

    
asked by Abraham Avendaño Veliz 24.06.2017 в 19:39
source

2 answers

0

I hope I have understood you well and that it will help you!

var numero = 0;

function incrementar() {
  let elementoDondeMostrar = document.getElementById("mensaje"); // Obtengo el elemento donde voy a mostrar el mensaje
  elementoDondeMostrar.innerHTML  = "Creditos " + numero; // Le pongo el contenido
  numero++; // Incremento la variable
}
<h5 id="mensaje"></h5>
<button onclick="incrementar()">Incrementa</button>

Greetings!

    
answered by 24.06.2017 / 19:51
source
0

It's just a matter of changing the innerHTML where you want it to be updated.

 credits.innerHTML=credit;

example:

var credit = 0;


    function addCredit(){

        credit = credit + 1;
        credits.innerHTML=credit;

    }

    addCredit();
<p>Créditos <span id='credits'></span><p>
<button onclick="addCredit()">Free Credits</button>
    
answered by 24.06.2017 в 19:52