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 Heading Text using JavaScript Current Date in mm/dd/yyyy Format JavaScript Current Date in dd/mm/yyyy Format in JavaScript JavaScript Current Date in yyyy-mm-dd Format JavaScript Get Date Difference in Hours JavaScript Get Date Difference in Minutes JavaScript Get Date Difference in Seconds Get Last Character of String with JavaScript Trigger Button Click On Page Load JavaScript Get id Of Child Element JavaScript Get Id Of Parent Element in JavaScript Get Table Row Index onclick in JavaScript Remove specific option from Select Tag with JavaScript Change Selected Option of Select Tag with JavaScript Get Length of Text In Textarea with JavaScript Change Text Color of Select Tag Option using JavaScript Change Heading Text in JavaScript Get Input Value Length in JavaScript Toggle Button Text with JavaScript Change Background Color of Dropdown with Dropdown using JavaScript Emoji Mood Slider with HTML CSS and JavaScript Hide and Show Div with Radio Button using JavaScript Count Options In Select Tag with JavaScript Get Select Tag Text OnChange with JavaScript Change Background Color OnClick using Input Field