2

I want to remove the button itself onclick. Can someone help me.

HTML:

<div id="hideButton">
    <a onclick="removeButton()" class="button1">PRESS</a>
</div>

Java Script:

function removeButton() {
    var element = document.getElementsById("hidebutton");
    element.classList.remove("button1");
  }

Every help is much appreciated :)

RBT
  • 24,161
  • 21
  • 159
  • 240
David S
  • 31
  • 6

3 Answers3

0

Use JavaScript's remove() function.

function removeButton() {
  var button = document.getElementById("hideButton");
  button.remove();
}
<div id="hideButton">
    <a onclick="removeButton()" class="button1">PRESS</a>
</div>
ahsan
  • 1,409
  • 8
  • 11
0

HTML

<div id="hideButton">
    <a onclick="removeButton(this)" class="button1">PRESS</a>
</div>

Javascript

function removeButton(buttonCntrl) {            
    buttonCntrl.remove();
}
Amit Verma
  • 2,450
  • 2
  • 8
  • 21
0

If you wished to apply this same logic to multiple button1 elements you can use a single external event listener bound to every instance of that class using querySelectorAll to find these elements.

document.querySelectorAll('a.button1').forEach(a=>a.addEventListener('click',function(e){
  this.parentNode.removeChild(this)
}));
.button1{
  border:1px solid black;
  width:120px;
  padding:0.25rem;
}

div{
  margin:1rem;
  width:100%;
}
<div>
    <a href='#' class="button1">PRESS #1</a>
</div>

<div>
    <a href='#' class="button1">PRESS #2</a>
</div>

<div>
    <a href='#' class="button1">PRESS #3</a>
</div>

<div>
    <a href='#' class="button1">PRESS #4</a>
</div>

<div>
    <a href='#' class="button1">PRESS #5</a>
</div>
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46