When ready, please slide me in.
In Part I, I wrote about how to retrieve the content of input fields, changing many elements on a Web page by using id, or class selectors. Now, I'll introduce you to the .ready() function and some of the animation functions found in the standard jQuery library.
So if you want to see something very useful and also pretty cool, please have a seat in the jQuery boat and we'll set sail on the next line.
jQuery .ready() Function
Figure 1: Adding the CSS makes the difference in the header description.
For a live example, click the links below to see the difference when executed in a browser.
Not using .ready()
Here, you see the .ready function in action, and you can view the source. At the end of this TechTip, you can download the examples.
Below are some examples showing how the .hide(), .show(), and .slideToggle() functions work. I have deliberately not included the .animate() function simply because I consider it a little over the top for this tip, but if you want to see it in action, click here.
jQuery has a lot of other animation functions, so try digging into it on the jQuery Web site. If the standard animations in jQuery are not enough, turn your eyes to the jQuery UI extension, which offers a wide selection of animations and a lot of other cool stuff.
Because this is a first glance, I won't use jQuery UI in this tip, but I will come back to it sometime next year and show you an introduction. If you cannot wait, point your browser to http://jqueryui.com/ and discover it for yourself.
Now, let's move on to see some useful animations at work.
.hide() will hide the matched elements. The elements can be hidden by using the slow/fast constants or by using a duration time, which is specified in milliseconds. You can, as always, use the jQuery selectors to hide things such as an HTML tag, an id, or a class.
.show() will, as you might have guessed, show the matched elements. It works under the same rules as the .hide() function.
Click here to see some examples of using the .hide() and .show() functions.
.slideToggle() allows you to you can display or hide, with a sliding motion, the matched elements.
Now you know the main rules, so I will not repeat myself about the selector thing.
Instead, let's look at an example I created; it's a Web page with music questions for you to answer. If you need help, you can click the Help link and some help text will be revealed before your very eyes. By clicking Help again, the text will vanish.
Figure 2: Let's quiz while testing the jQuery .slideToggle() function.
Click here to see this example of using the .hide() and .show() functions.
To download all the examples click here.
This was the second TechTip in my jQuery fundamentals series, and once again I hope it will make you say, "Hmm, maybe he's getting somewhere...."
In the next tip, I'll talk about "callback" functions and how they can help you in your programming life.
Till' next time, take a break from programming once in a while and listen to some good music.