JavaScript Button Pressed Only Once

ProVideoCreator

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>
PitaJ

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.

edited at
0

Comments

0 comments
Login to comment

Related

Button only allows clicking once

javascript reload only once

Labview button freezes after pressed once

the button works only once

Finish activity when back button pressed once

Javascript 'click' button only works once than have to refresh page

only to loop through the table heading once when add button is pressed, react js

How to give a function only once to a button, after another button was pressed?

Why is my spin animation only working once when button is pressed?

SDL drawing a sprite once a button is pressed

Javascript only firing once

Limit button click to only once

Only Once click Button Disabling using javascript

Javascript - Animation only working the first time the button is pressed

javascript run function only once per button click

Allowing a button to be pressed more than once

When button pressed or held, action only happens once

Animate only pressed button

Javascript Button Hover works only once

Flutter Button It should be pressed once in a day

I have a button in my react app that i only want it to be pressed once

How can I make it so the Tkinter button can only be pressed once?

Why Javascript menu opens only once when you click on the button?

Why Javascript menu opens only once when you click on the button?

how to get a button to update a counter only once when pressed the first time?

Tic tac toe javascript. Make it so X and 0 can only be pressed once, not overwrite eachother

html & javascript enter button redirects to error page - otherwise duplicates text to chat (only when enter is pressed)

All buttons are pressed even only one button is pressed in HStack SwiftUI

2 Form Submit buttons - Can I have a Javascript run when only one submit button is pressed