web analytics

How to Hide a Button in JavaScript

Creating a custom button on a web page is a common task. This is a guide on how to hide a submit button using the hidden attribute, style properties, and JavaScript functions.

Learn how to hide a button in JavaScript, you can follow these steps:

Step 1: Add the HTML Element

Start by adding a submit button within a div element on your web page. Assign an id to both the button and the div for easy reference.

<div id="buttonContainer">  
<input type="submit" id="submitButton" value="Submit">
How to hide a button in javascript image 1

Step 2: Use the Hidden Attribute

The hidden attribute can be used to hide buttons when the page loads. This attribute is a simple way to hide HTML elements without additional styling.

<input type="submit" id="submitButton" value="Submit" hidden>

Step 3: Write the JavaScript Function

Create a JavaScript function called hide. This function will change the style object of the submit button to make it invisible upon button click. The function will manipulate the style.display property and the style.visibility property of the button.

function hide() {  
var submitButton = document.getElementById('submitButton');
// Using style.display property
submitButton.style.display = 'none';
// Alternatively, using style.visibility property
// submitButton.style.visibility = 'hidden';

If you are wondering where to put the javascript function refer to W3School’s Tutorial.

Step 4: Add Button OnClick Event

Set up the button onclick event to call the hide function when the submit button is clicked. This can be done directly in the HTML element or by adding an event listener in your JavaScript.

Directly in the HTML:

<input type="submit" id="submitButton" value="Submit" onclick="hide()">

Or in JavaScript:

document.getElementById('submitButton').addEventListener('click', hide);

Step 5: Implement the Hide Function on Page Load

If you want to hide the submit button as soon as the page loads, you can call the hide function within a window.onload event:

window.onload = function() {

This will ensure that the submit button and any associated input fields are hidden immediately, without waiting for a user action.

Step 6: Review and Test Code

Finally, review your code. Ensure that it complies with your web page’s privacy policy. Also, make sure that it functions as intended across different browsers and devices.

Test the functionality to make sure that all elements are hidden or shown at the appropriate times.

By following these steps, you can effectively hide a custom submit button on your web page using the hidden attribute, the style.display property, or the style.visibility property. You can also ensure that it responds to user interaction such as a button click.

How to hide a div on button click in javascript

<div id="myDiv">This is the div to be hidden</div>

<button onclick="hideDiv()">Hide Div</button>


function hideDiv() {

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


How to hide a button in javascript image 2

How to hide image on button click in javascript

<img id="myImage" src="image.jpg">
<button onclick="hideImage()">Hide Image</button>
function hideImage() 
document.getElementById("myImage").style.display = "none";

3 examples why you would want to hide a button anyway

You may be asking yourself, why would I want to even hide a button anyway? Hiding a button on a web page can be useful for several reasons. Here are three examples:

1. Form Submission Workflow

In multi-step forms or complex workflows, you might want to guide the user through a series of inputs before allowing them to submit the form. Initially hiding the submit button can prevent premature submissions. The button can be revealed once the user has completed all necessary steps or entered all required information. This helps to ensure that the user does not skip important parts of the form and that the data collected is complete and valid.

2. User Access Control

You may want to hide buttons based on the user’s permissions or role. For instance, an ‘Edit’ or ‘Delete’ button should not be visible to a user who does not have the authority to perform these actions. Hiding these buttons can enhance security and user experience by preventing unauthorized actions and reducing interface clutter for users who do not need access to certain functionalities.

3. Dynamic Content Interaction

Buttons may control the display of dynamic content, such as a “Read More” button that expands a text section. Once clicked, it may be useful to hide the button to avoid confusion, as its action has already been completed. Alternatively, the button could be replaced with a “Read Less” button that collapses the text again. Hiding the initial button can make the interface more intuitive and prevent the user from attempting to repeat an action that no longer has any effect.

In each of these examples, hiding a button is used to improve the user interface and experience, streamline user interactions, and enforce appropriate access to functionalities within a web application.

Now you know how to hide a few different elements using JavaScript. Guess what comes with the territory of making scripts? Bugs and errors. Make sure you verse yourself in a common one, JavaScript foreach is not a function error.

You May Also Like…


How to Delete Pages in WordPress

Maintaining a streamlined and current website can often lead to the need to remove outdated or irrelevant content. If you’re managing a site using WordPress, the ability to delete pages is a straightforward procedure. It’s a...


Receive your free quote!

Receive Your Free Audit!