jQuery is a JavaScript library which helps make interactive webpages.
If you are completely new to jQuery, then please refer to:
- jQuery Library
- jQuery Syntax
- jQuery Events
Adding effects and animations to the webpage was a difficult task earlier. But, with the help of jQuery, we can add effects very easily. Effects help us make the webpage more interactive and beautiful. They also help us increase the quality of UX, if used at the appropriate place and appropriate time.
There are several jQuery effects. In this article, we will be starting with the hide, show, and toggle effect of jQuery. As the name suggests, hide effect helps us to hide some components, show allows us to show some components, and toggle allows us to toggle between hide and show.
Now, please write the following code in your editor and see the output.
- <html>
- <head>
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
- </head>
-
- <body>
-
- <h2>Click on any button</h2>
- <button id="hide">Hide</button>
- <button id="show">Show</button>
- <button id="toggle">Toggle</button>
-
- <br><br>
-
- <div class="myDiv">
- This will be affected.
- </div>
-
- <p>This will not have any effect on any button click, as it is outside myDiv and the effects are applied to class myDiv which is assigned to div
- </p>
- <script>
- $(document).ready(function(){
- $("#hide").click(function(){
- $(".myDiv").hide();
- });
- $("#show").click(function(){
- $(".myDiv").show();
- });
- $("#toggle").click(function(){
- $(".myDiv").toggle();
- });
- });
- </script>
- </body>
- </html>
The output should look like this:
Now, click on Hide button. The line "This will be affected" will be hidden. Then, click the show button. It will come back. And finally, click toggle. When you click toggle, if the line is visible, it will hide and if it is hidden, then it will show up.
Code Explained:
We are making a simple HTML page. Firstly, include jQuery. Then, there are three buttons - Hide, Show and Toggle. Following this, we have divisions with class "myDiv". And, finally we have a paragraph after the division myDiv.
In the script, we select the button using its id. So, as you can see, $("#hide") will select hide button. Then, we are using click event handler and defining a function in it. Inside the function, we are selecting myDiv using $(".myDiv") and adding hide effect to it, using the hide() method. Similarly, we are implementing the function for show and toggle buttons, by adding show() and toggle() methods to the respective functions.
We can also specify the speed with which the transition occurs. We need to specify the speed as parameter in the methods.
Example:
This will hide the division slowly.
This will show the division in fast speed.
This will trigger the toggle effect in 1000 milliseconds.
So, we can use slow, fast, and milliseconds, to specify the speed with which the effect occurs. This was a small overview of hide, show, and toggle effects in jQuery. In the next article, we will learn about the other effects, like animate, slide, fade, etc.
Stay tuned. Happy Learning!