JavaScript Hide Button After Click

In this tutorial we will see how to Hide Button After Clicking it using JavaScript. For this we have used the onclick event, JavaScript this and HTML DOM Style Object.

HTML Code

HTML Code is given below, This code contains HTML Button with onclick event.

<!DOCTYPE html>
<html lang="en">
<head>
<title>JavaScript Hide Button After Click</title>
</head>
<body>
 <button onclick="hideButton(this)">Click Me To Hide Me!</button>   
</body>
</html>

onclick event

The onclick event occurs when an HTML element is clicked. In this example onclick event handler is used to execute the JavaScript function once the button is clicked.

JavaScript Code

Take a look at the JavaScript code, this code is further explained below.

<script>
function hideButton(x)
 {
  x.style.display = 'none';
 }
</script>

Demo

JavaScript this

JavaScript this refers to an object, when used with event handlers JavaScript this refers to HTML element.

In this example JavaScript this is used to target button element.

HTML DOM Style Object

The HTML DOM Style Object is used to set or return the style properties of HTML element.

In this example, it is used to set the display property of button to none.

This will Hide the Button, once it is clicked.

Video Tutorial

Watch video tutorial on how to use JavaScript to Hide Button After Click.

Change Position of HTML Element using JavaScript How to Change Image on Hover with JavaScript How to Disable Button by Class Name in JavaScript How To Change Image Size with JavaScript How to change Image opacity with JavaScript How to Change image src on click with JavaScript How to get the lang attribute value in JavaScript How to Get attribute Value using JavaScript How To Check if Attribute Exists or not with JavaScript How To Count number of links on Page with JavaScript How To Scroll Page to Bottom using JavaScript How To Detect Shift Key Press with JavaScript Change Text Color On Hover with JavaScript Hide and Show div using JavaScript Get Button text with JavaScript Get textarea value with JavaScript Get table row Height with JavaScript Auto Increase width of input field with JavaScript Set Textarea maxLength with JavaScript Set Textarea Value with JavaScript JavaScript Count list items JavaScript set input field value Count Button Clicks with JavaScript Scroll Page to the Top on Click with JavaScript Change id of Element with JavaScript JavaScript Change li Text Color