There is a simple generator ( link to codepen ). How to hide the "ask" button after the alert and show the "new"?

function GetValue() { var myarray = new Array("word1","word2","word3"); var random = myarray[Math.floor(Math.random() * myarray.length)]; document.getElementById("message").innerHTML=random; }; //rand array element var clicks = 0; function onClick() { clicks += 1; document.getElementById("clicks").innerHTML = clicks; printMessage(document.getElementById("message").innerHTML,clicks); }; //count clicks function printMessage(random, clicks) { if (random === "word1") { document.getElementById("alert").innerHTML = "wow, after " + clicks + " clicks"; } } //display "you get word1 after _ clicks" function myFunction() { location.reload(); } //reload 
 <body> <textarea id="message" rows="1"></textarea> <input type="button" id="btnSearch" value="ask" onclick="GetValue();onClick()" /> <p>attempts: <a id="clicks">0</a></p> <p id="alert"></p> <button id="reload" onclick="myFunction()">new</button> 

  • one
    I do not see a single button in the code - Anton Shchyrov
  • Sorry, I forgot to add html. But I left a link to the codepen - Tarasovych
  • if (alert == true) {hide one button; show another button} do i think right? - Tarasovych
  • one
    You need when the condition if (random === "word1") hide one button and show the other. I understood correctly? And now you have both buttons displayed - is this how it was conceived? - Anton Shchyrov
  • Yes, you understood correctly! Now both are displayed, but I initially hid one through display: none (I already changed it in the codepen) - Tarasovych

1 answer 1

so like this

 function GetValue() { var myarray = new Array("word1", "word2", "word3"); var random = myarray[Math.floor(Math.random() * myarray.length)]; document.getElementById("message").innerHTML = random; }; //rand array element var clicks = 0; function onClick() { clicks += 1; document.getElementById("clicks").innerHTML = clicks; printMessage(document.getElementById("message").innerHTML, clicks); }; //count clicks function printMessage(random, clicks) { if (random === "word1") { document.getElementById("alert").innerHTML = "wow, after " + clicks + " clicks"; document.getElementById("btnSearch").style.display = "none"; document.getElementById("reload").style.display = "block"; } }; //display "you get word1 after _ clicks" function myFunction() { location.reload(); }; //reload 
 #reload { display: none; } 
 <textarea id="message" rows="1"></textarea> <input type="button" id="btnSearch" value="ask" onclick="GetValue();onClick()" /> <p>attempts: <span id="clicks">0</span> </p> <p id="alert"></p> <button id="reload" onclick="myFunction()">new</button>