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.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments