MC Press Online

Saturday, Jun 24th

Last updateFri, 23 Jun 2017 1pm

You are here: Home ARTICLES Programming Web Languages TechTip: jQuery Fundamentals, Part II

Programming / Web Languages

TechTip: jQuery Fundamentals, Part II



IBM i Security Administration and Compliance



Click for this Month's

Bookstore Special Deals

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

The execution of JavaScript/jQuery statements is done line by line, or asynchronously, or when it is ready for the browser to execute it. This means that if you place JavaScript code above code that refers to an element at the bottom of your page, you'll get an error. To prevent this scenario, you can use the .ready function, which will guarantee that the code is not executed before the DOM is ready. In plain English, if you want to do something to an object on your page and you want to be sure that the page is fully loaded, you should place your code inside the .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()


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.

jQuery Animations

Sometimes, you need to show or hide elements on a Web site or you need to fade out something when an action happens to an element. To do that using "normal" JavaScript can be a daunting task, but when using jQuery, it often can be done with a few lines of code.


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 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.

Jan Jorgensen

Jan Jorgensen is one of the owners of, which specializes in mobile and i5 solutions. He works with RPG, HTML, JavaScript, Perl, and PHP. You can reach him at