-2

Is there a way to hide a button with .innerHTML? I tried the following code but it did not work:

function F1() {
  bL1 = document.getElementById("bL1").innerHTML = '<body class="hidden">';
  ebL1 = document.getElementById("ebL1").innerHTML = '</body>';
}
.hidden {
  display: none;
}
<button onclick="F1()">Hide Lw</button>
<label id="bL1"></label>
<button> Lw </button>
<label id="ebL1"></label>
Save Pain
  • 247
  • 2
  • 9
  • 1
    Why use `innerHTML`? Why not simply [add the `hidden` class to your button](https://stackoverflow.com/questions/507138/how-do-i-add-a-class-to-a-given-element)? – Ivar Nov 21 '20 at 01:21
  • To hide the button, you should set the class name of the button to "hidden". – The KNVB Nov 21 '20 at 01:23
  • This is a simplified version of a complex situation, where adding a class directly to the button is not wanted. Adding the class must happen via innerHTML – Save Pain Nov 21 '20 at 01:50

1 Answers1

1

If you are trying in this method you are doing very wrong but use the following code that should work correctly to hide a button or anything you want to hide.

function F1() {
  bL1 = document.getElementById("bL1").style.display = 'none';
}
<button onclick="F1()">Hide Lw</button>
<button id="bL1"> Lw </button>

This is a very easy and short method to apply CSS inside JavaScript.

Emma
  • 27,428
  • 11
  • 44
  • 69