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.

Get Last Element Of Array Without Removing It Remove Last Element From Array JavaScript Remove First Element From Array in JavaScript Tic Tac Toe Game in JavaScript Remove Class from HTML Tag OnClick JavaScript JavaScript Select li Tag By Index Display JavaScript Variable In HTML Insert HTML Code anywhere With JavaScript JavaScript Date Difference in Days JavaScript Hide Button After Click Simple Click and Hold Effect Using JavaScript Remove Class From All Elements in JavaScript JavaScript Select All HTML Elements Whose ID Start With Same String Count Checked Checkboxes of Specific Form with JavaScript Enable and Disable Button using JavaScript Add Class to HTML Tag On Click using JavaScript Change Class of HTML Tag Using JavaScript Get All Options In Select Tag using JavaScript Replace a Word from everywhere in a String using JavaScript Execute a Function on Hover Using JavaScript