How do I make it so that using the code below, you can only click the button once, before it doesn't do anything. Thanks in advance!
<button onClick="yumpizza();"style="position:absolute; TOP:120px; LEFT:350px">Roll for strength</button>
<script>
var strength = Math.floor(Math.random() * 20 + 1);
function yumpizza() {
document.getElementById("pizza").innerHTML = ("Strength:" + strength)
}
</script>
Just set the onclick event of the element to null after the first trigger:
<button id="pizzaButton" onClick="yumpizza();" style="position:absolute; TOP:120px; LEFT:350px">Roll for strength</button>
<script>
var strength = Math.floor(Math.random() * 20 + 1);
function yumpizza() {
document.getElementById("pizza").innerHTML = ("Strength:" + strength);
document.getElementById("pizzaButton").onclick = null;
}
</script>
On the note of event handling, is is a best practice to use addEventListener
to set event handlers programmatically, instead of the onclick
HTML attribute.
Add one more line to yumpizza() function:
document.getElementById("pizzaButton").disabled = true;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With