skip to Main Content

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. Help asap thank you very much!

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

2

Answers


  1. Use:

    this.style.display='none';

    within onclick inside the div you want to remove:

    <body>
      <div>
        <button type='button' onclick="alert('message1'); this.style.display='none';">button1</button>
        <button type='button' onclick="alert('message2')">button2</button>
      </div>
    </body>
    
    Login or Signup to reply.
  2. Maybe you use this.disabled='true';
    I’d recommend you this code.

          style="
            position: absolute;
            left: 625px;
            top: 100px;
            background-color: white;
          "
        >
          <button
            type="button"
            id="btn1"
            onclick="alert('The guy walks away disapointed, I think I made the wrong decision') ; document.getElementById('btn2').disabled='true'; this.disabled='true'"
          >
            Detain
          </button>
          <button
            type="button"
            id="btn2"
            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'; this.disabled='true'"
          >
            Free them
          </button>
        </div>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search