Getting Started with Jquery

Access jQuery

The first step when using jQuery is to connect your webpage to the jQuery library by either downloading and linking to a jQuery file that you own and control, or by linking to someone else’s jQuery file that is already online. For these exercises you will download files that are already linked directly to the latest version of jQuery directly from a site provided by Google, thanks to this line of code, as you’ll see when you download the exercise files:  

<script src=""></script>

Download Exercise Files

1. Visit, download the file named and unzip it.

2. Drag the jq1 folder into your mysite folder.


We will start our exploration of jquery with a tool called SlideToggle that employs a built in animation to hide and reveal content.   

1. Open the file named slideToggle.html with your text editor (most likely Brackets). 

2. Inside of the head tag you should see the code below. The first two lines describe a CSS style named #content that defines a div tag.  The third line links to the jQuery library (jquery-latest.js) that we need to perform all of the upcoming magic.

<style>    #content { width:600px;
<!--display: none;-->  }  </style> 
<script src=""></script>

Take note as well that the second line display: none, which would make the div tag invisible. However, this line has been commented out. Later on we will remove that comment.

3. Now look directly below the <body> tag at the line of code that says:   <button>Reveal (or hide) the Monkey</button> Click Live View to see what that button looks like in the wysiwyg view. (Wondering what wysiwg means? Here’s a quick definition). 

4. Look at the next line of code that creates a div tag and places a rhesus monkey inside:
<div id=”content”><img src=”monkee4.jpg” width=”579″ height=”368″ alt=”monkee”></div> 

5. The next four lines, shown below contain the jQuery commands that control the #content div tag. Let’s dissect them.b1

a) Take note of the <script> and </script> tags that bracket the javascript statement. The purpose of the first tag is to inform the browser that there is javascript ahead; the </script> tells the browser that the javascript has just ended.

b) The second line, the one that starts with $(“button”).click instructs the browser to instigate a function whenever someone clicks the “Reveal (or hide) The Monkey” button. Want to know more about the $ identifier JavaScript? Check out this article.

c) The next line defines the function. It instructs the browser to apply slideToggle to the #content div tag and to do it slow(ly).

d) By the way, this particular function is what is known as an “anonymous function”. Anonymous functions have no name because they are designed to automatically operate on a designated element in a webpage. This particular function operates whenever you click ANY button in the webpage. Read more on anonymous functions.

6.  Time to play a bit. Preview the file in your browser and click the “Reveal (or hide) The Monkey” to see what happens. The monkey should slowly disappear. Click again; the monkey should reappear.

7. Change the speed from “slow” to “fast” and click the button a few more times. Did the monkey speed up? As it turns out, the “slow” setting is set to 600 milliseconds while “fast” is set to 200 milliseconds.

8. Now that you know about milliseconds you can fine tune the speed of the animation. Change the speed to 5000 as in slideToggle(5000); and click the button again. The monkey should slow down dramatically and take 5 seconds to appear or reappear. Now change the number to 100. The animation should now be really fast. 

9. Reset the animation speed to “slow”.
Take a look at the CSS that defines the #content div tag and remove the comment from <!–display: none;–>  so that it reads display: none;

10. Now the monkey should disappear by default but reappear once you click the button. Congratulations! You have a monkey that toggles.

11. So far we have been revealing and hiding a monkey picture but be aware that the slideToggle effect applies to whatever is inside the div tag. Delete the monkey and type a bunch of text inside of the div tag. Now click the button. The text should be have just like the monkey.

12. Functions such as SlideToggle can also be tied to other html and CSS elements such as <p> (paragraph) tags or CSS class tags.

(a) To demonstrate, delete the code that defines the div tag ( <div id=”content”>)  and replace it with <p> . Next, delete </div> and replace it with </p>.

(b) Now target the <p> tag by changing  $(“#content“).slideToggle(“slow”);  to $(“p“).slideToggle(“slow”);

(c) Click the button and everything should work as before. By the way, if you add additional <p> tags, all will disappear and reappear simultaneously, which should indicate that id tags offer better flexibility! Review id attributes. This way, you can target specific elements rather than just all of your paragraphs.

SlideToggle is just one of many methods for revealing and hiding content that jQuery offers. Others (all of which do more or less what their names indicate) include slideUp, slideDown, fadeIn, fadeOut. Examples of all of these methods and more are available at