jQuery Hands-on
Intro to jQuery and External JavaScript Files
Intro to jQuery and External JavaScript Files

The Write Less, Do More, JavaScript Library
jQuery is a cross-browser JavaScript library designed to simplify client-side scripting, manipulating HTML pages, animating and handling events.
jQuery is still JavaScript. It is a library of functions built on top of JavaScript to simplify your program. It does, however, have it's own syntax as well.
The library contains many, many built-in functions and methods. It's not an easy task to remember them all. Never fear, jQuery has documented their API here: http://api.jquery.com.
Let dive in and see how jQuery methods compare to using just JavaScript alone.
Including inline JavaScript into your web page only requires the code to be written between the <script></script> tags.
In jQuery, the document needs to be "ready". This bit of code needs to wrap all of your jQuery: $(document).ready(function(){});. Your code goes between the curly braces {}, and will only run once the DOM is ready.
<script type="text/javascript">
// JavaScript code here
</script>
<script type="text/javascript">
$(document).ready(function(){
//jQuery code here
});
</script>
Remember document.write()? The functionality is similar. document is the object, write() is the method we use to pass in parameters. (see slide)
In jQuery syntax, the $ is simply a shorcut for a function called jQuery. It is how you access all of the functionality in the library. To select the document jQuery style, it looks like this:
$(document)
.ready() specifies a function to execute when the DOM is loaded.
$(document).ready();
An "anonymous function" needs to be passed into the above code to execute the jQuery code.
//syntax for anonymous function
function(){
code goes here
}
// pass the anonymous function in the ready() method
$(document).ready(function(){
jQuery code here
});
Remember document.getElementById?
jQuery makes it easier to reference objects in the DOM.
<div id="status"> <p>Loading...</p> <p><img src="assets/loading.gif"></p> </div>
// JavaScript
document.getElementById("status")
// jQuery
$("#status")
Your HTML tag must have an id attribute in order to do this!
Use #name to reference and ID and .name to reference a class
Let's see how the JavaScript innerHTML example can be done with jQuery
to replace what's already in an element.
You can also use "dot notation" to write the statement in one line instead of declaring a variable.
Keeping it organized, working with others
and adding libraries
As you write bigger pieces of JavaScript, you will want to start putting your code into their own files so they are easier to manage. Or you might want to enlist the help of others to write JavaScript for your website so you can work on different sections separately. A big site could have thousands of lines of code written by many different people!
To link to an external JavaScript file:
<html>
<head>
<title>JavaScript example!</title>
<script type="text/javascript" src="scripts.js"></script>
</head>
<body>
<div id="example">This is an example.</div>
</body>
</html>
In the external JavaScript file (scripts.js) you don't include script tags; just JavaScript code.
View this example: http://gavinuhma.github.io/LLC-JavaScript/exercises/external-javascript.html
Even though you're not writing JavaScript between the script tags, you must use a closing </script> tag. A self-closing tag such as <script src="scripts.js" /> will not work. The correct way is highlighted below:
<script type="text/javascript" src="scripts.js"></script>
To make use of the jQuery library, it needs to be included on your web page using an external file. There are two ways to include the file.
1. Download the jQuery library and include it with the rest of your files.
<script type="text/javascript" src="jquery.js"></script>
2. Link directly to the CDN (Content Delivery Network) hosted copies.
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
Download instructions: http://docs.jquery.com/Downloading_jQuery
When adding your own custom-written jQuery/JavaScript files, make sure to include it after the jQuery file to ensure that the library loads first.
<script type="text/javascript" src="jquery.js"></script> <script type="text/javascript" src="custom.js"></script>
Selectors are always surrounded by single or double quotes, except when referencing a variable.
<div class="name">Class selector<div>
<div id="name">ID selector<div>
$(".name") - referencing a class
$("#name") - referencing an ID
$("div") - referencing an HTML element
var selector = $("name");
$(selector) - referencing a variable
Just like Javascript functions, we can execute the code on an event. A very common event is the click event.
$(selector).click(function(){
//code to execute
});
click() triggers a click event, the selector is the element to be clicked and function goes between the parenthesis to run a function, containing one more commands.
jQuery may have its own syntax and methods but it's still JavaScript at
its core. Let's look at a previous example and see how jQuery and JavaScript work together.
In the exercises folder, open jquery-javascript.html and jquery-javascript.js.
While it works using pure JavaScript, the functions are currently being called "inline" by adding an onclick directly in the element. It's best practice to avoid inline event handlers.
Let's use the jQuery click() method instead and use it to select the element and pass the related function.
There are many handy built in methods that make it simpler to add effects. The full list is available here. It's searchable and provides explanations and examples.
Here's an example of how to use the .hide() and .show() methods.
Let's practice adding some more jQuery effects and using external javascript files.
<a> links, to add the following effects:
show()hide()slideDown()slideToggle()fadeOut()fadeIn()jQuery effects documentation: http://docs.jquery.com/Effects
jQuery click event documentation: http://api.jquery.com/click/
Solutions posted in the solutions folder.