Buttons are one of the most common UI elements used in web development. At times, you may need to hide a button after it has served its purpose, like submitting a form, making a payment, etc. JavaScript provides easy methods to hide buttons dynamically.

In this comprehensive guide, we will explore the various techniques to hide buttons using plain JavaScript:

Why Hide Buttons in JavaScript?

Here are some common reasons why you may need to hide buttons on your website:

  • Prevent multiple form submissions: Hiding the submit button after clicking it once prevents duplicate form submissions.

  • Show state change: For example, you can hide the “Add to Cart” button and show a “Product Added” confirmation message after the user clicks it.

  • Toggle visibility: You may want to show/hide buttons based on certain conditions or user actions.

  • Disable access: Hide deletes or back buttons to prevent unwanted actions.

Hiding buttons dynamically enhances user experience and gives you more control over the interface.

Methods to Hide Buttons

There are three main methods to hide buttons with JavaScript:

  1. Changing the display property
  2. Changing the visibility property
  3. Removing the button element from DOM

Let‘s explore each of them in detail with code examples.

1. Change Display Property to Hide Button

The easiest approach is to set the display style property to none.

For example:

document.getElementById("myButton").style.display = "none"; 

This completely hides the button as if it doesn‘t exist on the page.

Here is a simple demo:

<button id="myButton">Click Me!</button>

<script>
function hideButton() {
  document.getElementById("myButton").style.display = "none";
}
</script>

When you click the button, the hideButton() function sets display: none which hides it instantly.

Advantage

  • Quick and simple way to toggle button visibility.

Disadvantage

  • Still occupies space on the layout since it remains in the DOM flow.

2. Change Visibility Property to Hide Button

Another approach is setting the visibility style property to hidden:

document.getElementById("myButton").style.visibility = "hidden";

This hides the button visually but still keeps its space allocation on the page layout.

For example:

<button id="myButton">Click Me!</button>

<script>
function hideButton() {
  document.getElementById("myButton").style.visibility = "hidden"; 
}
</script>

When you invoke hideButton(), the button becomes invisible but space for it is reserved.

Advantages

  • Button is simply hidden without affecting layout.
  • Can show button again easily by setting visibility: visible.

Disadvantage

  • Space is allocated for the hidden button.

3. Remove Button from DOM to Hide

You can also completely remove the button element from the DOM itself:

var button = document.getElementById("myButton");
button.parentNode.removeChild(button);

This deletes the button node making it disappear from the visible UI.

For example:

<button id="myButton">Click Me</button>

<script>
function hideButton() {
  var btn = document.getElementById("myButton");
  btn.parentNode.removeChild(btn);
}
</script>

When hideButton() runs on click, it removes the button element from DOM.

Advantages

  • Frees up space by removing element.
  • Greater control over managing component.

Disadvantages

  • Cannot easily show button again.
  • Need to recreate dynamic components.

Now that you know the basics, let’s go over some practical examples.

Hide Button on Click using JavaScript

A common use case is to hide a button after a user clicks on it.

For example, you may want to hide the Submit button on a form after it is clicked to prevent duplicate submissions.

Here is a simple example:

<form>
  <button onclick="hide()">Submit</button>
</form>

<script>
function hide() {
  document.getElementsByTagName("button")[0].style.display = "none";  
}
</script>

This hides the first <button> inside the form when clicked by setting display:none.

Some key points:

  • Apply onclick="hide()" directly on button to trigger function.
  • Get button using document.getElementsByTagName("button").
  • Index first element with [0] to target that specific Submit button.
  • Set display to "none" to hide it.

This approach works well for simple scenarios like forms.

Toggle Button Visibility on Click

Sometimes you may need to toggle between showing and hiding a button dynamically.

Here is an example to demonstrate:

<button id="my-button">Show / Hide</button>

<script>
var button = document.getElementById("my-button");
var hidden = false;

button.onclick = function() {

  if(!hidden) {
    button.style.display = "none";
    hidden = true;
  }
  else {
    button.style.display = "inline-block";
    hidden = false; 
  }

}  
</script>

Here we define a hidden flag to track state. On each click:

  • Check if currently hidden
  • If yes, show button by setting display
  • Else hide button by setting display: none
  • Also update the state flag accordingly

This approach gives you full control to dynamically show/hide any button.

Hide Submit Button on Form Validation

For important forms, you need to perform client-side input validation before submitting it. In such cases, you can hide Submit button until validation passes.

For example:

<form>
  ...
  <input type="text" id="username">

  <button>Submit</button> 
</form>

<script>

  var $submit = document.getElementsByTagName("button")[0];  

  function validateForm() {
    // Assume validation logic
    return usernameValid && emailValid; 
  }

  if(!validateForm()) {
     $submit.style.visibility = "hidden";
  }

</script>

Here we check validation on pageload and hide Submit if invalid. This prevents unusable form submission.

Some key points:

  • Check validation inside validateForm()
  • Hide button by setting visibility: hidden
  • Can rerun check on field change to re-enable

This validator integration keeps unusable forms from getting submitted.

Conclusion

With the power of JavaScript, you can easily show and hide buttons dynamically on your websites.

Here are some key takeways:

  • Use display: none to completely hide buttons.
  • visibility: hidden simply visually hides them.
  • Remove the element from DOM if you want to delete buttons.
  • Attach onclick handlers to toggle display property.
  • Check form validation before showing submit buttons.

Hiding buttons gives you finer control over the interface functionality. It helps prevent unwanted user actions and enhances overall UX.

I hope this detailed guide helps you use the various techniques to show and hide buttons using plain JavaScript!

Similar Posts