Search code examples
javascripthtmlbuttonclick

How to make a button clickable once?


In my code I want a button to be clickable once. I put 2 buttons below me because I want to disable the other button for use when one of the buttons are clicked.

<div style = "position:absolute; left:625px; top:100px; background-color:white;">
    <button type="button" onclick="alert('The guy walks away disapointed, I think I made the wrong decision')" >Detain</button>
    <button type="button" onclick="alert('The guy sighs in relief and looked happy. The guards guide him to the exit. I think I made the right decision')">Free them</button>
         </div>

Solution

  • give every button a unique id then disable the other button on the click like in the following example

        <div style = "position:absolute; left:625px; top:100px; background-  color:white;">
    <button id="btn1" type="button" onclick="alert('The guy walks away disapointed, I think I made the wrong decision'); document.getElementById('btn2').disabled = true;" >Detain</button>
    <button id="btn2" type="button" onclick="alert('The guy sighs in relief and looked happy. The guards guide him to the exit. I think I made the right decision'); document.getElementById('btn1').disabled = true;">Free them</button>
         </div>