-2

I want my code to only peform the onlick before a button is pressed. How can I remove the onclick so that they don't pass anything?

<!-- remove onclick -->
<div class="boardsection2" id="2_8_7" onclick="divclick('2_8_7')"></div>

<!-- button to execute the removal of onclick -->
<button class="testclass" id="testid" onclick="test()">finish setup</button> 

I tried with

document.getElementByClassName("boardsection1").removeAttribute("onclick")

but it did not affect anything.

  • 1
    use addEventListener() and removeEventListener() instead of onclick – Asphodel Jun 17 '23 at 17:49
  • Or, unless it's _extremely_ clear why the button is disabled, change the click handler to one that gives a message why it cannot be used. If it's never going to be used again, you could remove the button entirely. – Darryl Noakes Jun 17 '23 at 18:44

2 Answers2

2

Assign null to the onclick property:

document.getElementById("boardsection1").onclick = null;
Tamir Abutbul
  • 7,301
  • 7
  • 25
  • 53
Barmar
  • 741,623
  • 53
  • 500
  • 612
-2
<button class="testclass" id="testid" onclick="test()">finish setup</button>

<script>
  let button = document.getElementById("testid");
  button.removeAttribute("onclick");
</script>