You can also toggle between hiding and showing an element by using toggle() method.
Shown elements are hidden and hidden elements are shown:
The optional speed parameter can take the following values: "slow", "fast", or milliseconds.
The optional callback parameter is a function to be executed after toggle() completes.
Syntax:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>toogle() jQuery Method</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<style>
h2 {
color: white;
text-align:center;
padding-top:40px;
}
</style>
<script type="text/javascript">
$(document).ready(function(){
$("#button").click(function(){
$("#div").toggle();
});
});
</script>
</head>
<body>
<button id="button">Show/Hide</button>
<br>
<br>
<div id="div" style="width:400;height:100px;display:none;background-color:green;"><h2>STUDENTS TUTORIAL</h2></div>
</body>
</html>
Run it Yourself »