How to make a text appear when JS condition is met

1

I'm starting with js and I have a doubt when it comes to printing text in the DOM with js.

I have a button that adds + 1 to var click and I want that when the number of click reaches 5, it appears the text of the function sumar()

var click = 1;
document.getElementById("demo").innerHTML = click;

function sumar() {
  click = click + 1;
  document.getElementById("demo").innerHTML = click;
}
if (click > 5) {
  function sumar() {
    document.getElementById("demo2").innerHTML = "Es más que 5";
  }
}
<h3 id="demo"></h3>
<h3 id="demo2"></h3>
<button id="button" onclick="sumar()">Click</button>
    
asked by Alejandro 19.04.2018 в 15:07
source

1 answer

1

What happens is that in your js I was not calling the function masque (). Try this:

var click = 1;
document.getElementById("demo").innerHTML = click;

function sumar() {
  click = click + 1;
  document.getElementById("demo").innerHTML = click;
  if (click > 5) {
    masque();
  }
}

function masque() {
  document.getElementById("demo2").innerHTML = "Es más que 5";
}
<h3 id="demo"></h3>
<h3 id="demo2"></h3>
<button id="button" onclick="sumar()">Click</button>
    
answered by 19.04.2018 / 15:15
source