The jQuery library was developed to reduce Javascript code lines. If you hide and show a div block using JavaScript only then it will be very complex but if you hide and show a div block using jquery then it will be very easier.
Hide and show div block on a single button click in jQuery -
As you know, we can hide and show dive block using class name and id. We create a function on button click using button id or class. In this example, we will create a function click using button id.
Did you use the toggle() function in the website menu?
Yes, we will use a simple jQuery toggle() function to hide and show a div block on single button click.
<!DOCTYPE html>
<html>
<head>
<title>How to hide and show div on button click in jQuery?</title>
<script src="https://code.jquery.com/jquery-3.6.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$( "#my_btn" ).click(function() {
$("#my_div").toggle();
});
});
</script>
</head>
<body>
<div class="my_div" id="my_div">This is my div block.</div>
<button type="button" class="btn btn-primary" id="my_btn">Hide and Show</button>
</body>
</html>
In the above code, you can see, I have used only jQuery toggle() function to hide and show a div block using the button id name and div block id name.