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.
Table of Contents
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.