I’m making a hint button that once used twice it will set its display to none but finding it very hard to figure out a way on how to do so. I tried using var that once the var has hit 2 it will set display none but it has only set the var to 1 and then 1 not 1 then 2 I used
function clicked()
{
var y = 0;
let text;
let hint = "are you sure you want to use a hint?";
if (confirm(hint) == true) {
document.getElementById("select").style.display = "block";
document.getElementById("buttonenter").style.display = "block";
y++;
document.getElementById("counter").innerHTML = y;
if (y = 2) {
document.getElementById("hint").style.display = "none";
}
}
but that didn’t work
I tried setting up a counter but every info I found about it I didn’t understand how to tweak it to work for this.
function clicked()
{
var y = 0;
let text;
let hint = "are you sure you want to use a hint?";
if (confirm(hint) == true) {
document.getElementById("select").style.display = "block";
document.getElementById("buttonenter").style.display = "block";
document.getElementById("counter").innerHTML = y +1;
if (y = 1) {
document.getElementById("hint").onclick = function Why()
}
}
function enter() {
var x = document.getElementById("select").value;
document.getElementById("demo").innerHTML = x;
if (x == "4") {
document.getElementById("demo").innerHTML = "c______l ____s";
document.getElementById("select").style.display = "none";
document.getElementById("buttonenter").style.display = "none";
}
else if (x == "6") {
document.getElementById("demo").innerHTML = "pho__";
document.getElementById("select").style.display = "none";
document.getElementById("buttonenter").style.display = "none";
}
else if (x == "7") {
document.getElementById("demo").innerHTML = "bottle, eyes";
document.getElementById("select").style.display = "none";
document.getElementById("buttonenter").style.display = "none";
}
else {
document.getElementById("demo").innerHTML = "Thats not a level";
document.getElementById("select").style.display = "none";
document.getElementById("buttonenter").style.display = "none";
}
}
.hide {
display: none;
}
.buttonenter {
display: none;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>replit</title>
<link href="style.css" rel="stylesheet" type="text/css" />
<script src="script.js"></script>
</head>
<hint><button id="hint" onclick="clicked()">Click me for a hint</button></hint>
<input class="hide"id="select"type="text">
<text id="demo"></text>
<button class="buttonenter" id="buttonenter" onclick="enter()">Submit</button>
</body>
</html>
for some reason snippet doesn’t work
2
Answers
You can store the counter variable outside of your function. Here is an example of a button that dissapers on the second click.
As an alternative, you can store the counter in an element. Here an example where the counter is visible. But you can also set the visibility of it to hidden.
Here is a possible solution.