Hide/Show on click jquery

When it comes to hiding or showing a div on click, jQuery is a great tool to do so. To show a div element on click, you can use the .show() method of jQuery. For example, if you have an element with the ID "myDiv", you can use the following code to show it when a button is clicked:


$("#myButton").click(function(){
   $("#myDiv").show();
});

The above code will look for an element with the ID "myButton", and if it is clicked, it will show an element with the ID "myDiv". To hide a div element on click, you can use the .hide() method of jQuery. Again, if you have an element with the ID "myDiv", you can use the following code to hide it when a button is clicked:


$("#myButton").click(function(){
   $("#myDiv").hide();
});

The above code will look for an element with the ID "myButton", and if it is clicked, it will hide an element with the ID "myDiv". If you want to toggle the visibility of an element on click, you can use the .toggle() method of jQuery. Again, if you have an element with the ID "myDiv", you can use the following code to toggle its visibility when a button is clicked:


$("#myButton").click(function(){
   $("#myDiv").toggle();
});

The above code will look for an element with the ID "myButton", and if it is clicked, it will toggle the visibility of an element with the ID "myDiv".

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe