navigate site menu

Start learning with our library of video tutorials taught by experts. Get started

jQuery Essential Training

jQuery Essential Training

with Joe Marini

 


In jQuery Essential Training, Microsoft professional Joe Marini presents the power of the jQuery library, an open-source JavaScript project that greatly simplifies the process of adding advanced functionality to web sites. Joe teaches how to use these new features to build pages that work across browsers with the functionality that today's users (and clients) are looking for, from complex animation effects to dynamic page formatting. Joe pulls all of this together, showing how the jQuery UI plug-in can expand and streamline the capability of jQuery, and then integrating jQuery design tools into a complete sample web site. Exercise files accompany the course.
Topics include:
  • Constructing jQuery selectors and filters to gather information from web pages
  • Creating, inserting, and manipulating web page content
  • Understanding jQuery statement chaining
  • Building event handlers that work across browsers
  • Working with jQuery effects, such as showing, hiding, and fading page elements
  • Creating custom animations with specialized properties and options
  • Using the jQuery UI plug-in to give pages a polished look

show more

author
Joe Marini
subject
Developer, Mobile Apps, Web, Mobile Web, Web Development
software
Ajax , jQuery , jQuery Mobile
level
Beginner
duration
4h 52m
released
Sep 01, 2009
updated
May 24, 2013

Share this course

Ready to join? get started


Keep up with news, tips, and latest courses.

submit Course details submit clicked more info

Please wait...

Search the closed captioning text for this course by entering the keyword you’d like to search, or browse the closed captioning text by selecting the chapter name below and choosing the video title you’d like to review.



Introduction
Welcome
00:00(Music playing.)
00:03Hi! I am Joe Marini.
00:05I would like to welcome you to jQuery Essential Training.
00:07In this course, I'll show how to use jQuery, a free JavaScript Library, to
00:11dramatically reduce the amount of code that you to write in order to build web
00:15pages that are modern responsive and automatically work across browsers.
00:19You'll see how jQuery can be used to extract information from web pages using a
00:24variety of simple CSS like statements.
00:26We'll explore how jQuery makes the creation of content manipulation and event
00:31handling code work across browsers.
00:33I'll show you how to use jQuery to provide advanced user interface elements
00:36to your website users, and build smooth effects that used to require plug-ins to accomplish.
00:40Whether you are new to writing JavaScript code or a grizzled veteran web
00:45developer looking to improve your productivity and reduce browser specific code,
00:49you'll find jQuery to be a great tool to create web pages that are
00:52polished, professional, and engaging.
00:55There is a reason they call jQuery the "write less, do more" library.
00:59So if you are ready to take your websites to the next level of interactivity,
01:02while dramatically reducing the amount of code you have to write in order to get there,
01:06then let's get started with jQuery Essential Training.
Collapse this transcript
Using the exercise files
00:01If you are a premium member of lynda. com Online Training Library, or if you are
00:06watching this tutorial on a DVD-ROM, then you have access to the exercise files
00:12used throughout this title.
00:14So let me open the exercise_files folder.
00:17The exercise files are laid out according to the chapter.
00:21So for each chapter, I have included the exercise files I have used in that chapter.
00:27So let's take a look at an example.
00:30Essentially the exercise files follow a couple of different conventions.
00:34In some cases you'll see that I have got exercise files labeled _start and _finished.
00:40In that case, the finished version contains the fully working example, which
00:45you can open in your browser and look at, or open in your code editor to see how it works.
00:51And the start version contains the starting point for writing the code necessary
00:56to get to the finished state.
00:58So if you want to open the start version and follow along with me in each one
01:03of the tutorials, you can write the code along with me and arrive at the finished state.
01:08Other files are simply complete examples that we deconstruct during the lesson,
01:13so that you can see how they work.
01:15For example, this file here is already in the finished state.
01:18There is no starting state, and for that particular lesson we simply open the
01:22file in the code editor and examine how it was written and why things work the way they do.
01:29Now if you are a monthly subscriber or an annual subscriber to lynda.com,
01:33you don't have access to these exercise files.
01:36But you can follow along from scratch or using your own assets.
01:41Okay, let's get started.
Collapse this transcript
1. Overview of jQuery
What is jQuery?
00:01Okay so, if you are ready as I am to write less and do more, then let's just
00:06dive right in and get started with jQuery Essential Training.
00:10Let's start by talking about what jQuery is.
00:13jQuery is a freely available Open Source JavaScript Library and its purpose in
00:18life is essentially to simplify the task of creating modern web pages that are
00:23highly interactive and responsive, essentially full of all the Web 2.0 goodness
00:28that you see all over the internet today.
00:31It works across all the modern browsers.
00:33One of the great things about jQuery is that it abstracts away a lot of the
00:38browser-specific features, which allows you to concentrate on the design and
00:44finished result that you are trying to achieve rather than spending all your
00:47time trying to figure out how to achieve various effects and features within
00:51individuals browsers.
00:53So jQuery focuses on doing a few things very well.
00:57For example, jQuery makes it really easy to get and manipulate page content.
01:04Now normally you do this using the Document Object Model or the DOM.
01:08If you are not familiar with the DOM, then lynda.com has a great title known as
01:13JavaScript Essential Training by Dori Smith, and you can go check that out to
01:17learn more about the DOM.
01:18jQuery makes that really simple.
01:19jQuery also makes it really simple to work with the modern browser event model,
01:26and again this is a key part of building modern Web 2.0 applications.
01:32Finally, jQuery adds a pretty sophisticated effects library that you can use
01:37to achieve a lot of sophisticated transitions and effects that you see in
01:42modern websites today, things like image fades and animations and events and
01:46that kind of stuff.
01:48If you take a look at how most modern web development is done today, there are a
01:53couple of scenarios that seems to pop up over and over again.
01:56They typically follow a couple of very common patterns.
01:59The first pattern is usually when the page loads in the browser, there is a
02:03bunch of setup that you have to do, and this is usually in response to the
02:08window load event, and jQuery provides a really efficient way of handling this.
02:13The second scenario that pops up a lot looks something like this.
02:16There is an event that happens on the browser.
02:18The user clicks on something or they type a key or they move the mouse over
02:22something, and that sets off a reaction, something along the lines of,
02:26you retrieve the content from the page, then you do some kind of manipulation on it.
02:30Maybe you animate it somehow and then you put the content back in the page.
02:34Now this may not be followed exactly, but the pattern is essentially along these
02:39lines where the user does something, and in response to that event you have to
02:43somehow get content and manipulate it.
02:45And jQuery makes these scenarios really, really easy.
02:49Some of the benefits of using jQuery are first, jQuery leverages your existing knowledge of CSS.
02:57So if you are already familiar with CSS, then you will see this throughout the course.
03:01You'll find yourself writing syntax that looks a lot like CSS code, which makes
03:05it really easy to pick up some of the jQuery's best features.
03:08jQuery is also built from the ground up to work with sets of elements.
03:14This is something that Document Object Model in JavaScript is not designed to do
03:18very well. jQuery makes it second nature.
03:21In fact, many times you won't even realize that you are writing code that just
03:25by default works across sets of elements.
03:27jQuery also allows you to perform multiple operations on a set of elements
03:32with just one line of code, and this is a very powerful feature known as
03:36Statement Chaining.
03:37You'll see this again and again throughout the course.
03:39It really simplifies the way that you work with JavaScript and elements in
03:43the page and it's one of the jQuery's best features and we'll see that
03:47throughout the course.
03:48Now as I mention before, jQuery hides away a lot of the various browser quirks
03:53and this allows you to concentrate on your end result.
03:56So you don't spend a lot of time trying to figure out how to achieve each effect
04:01within each individual browser.
04:02You simply write to the jQuery interface and jQuery handles all of the
04:07individual browser features behind the scenes.
04:10Finally jQuery is really extensible.
04:12There are a lot of plug-ins available that perform all kinds of web development tasks.
04:18In fact, it's pretty easy to learn how to write your own, but in many cases you won't have to.
04:23The jQuery website contains hundreds of plug-ins that have written by other people,
04:27that you can just freely download and use in your own pages.
04:31jQuery is currently compatible with modern versions of all the main browsers
04:36that are in use today.
04:38So you can use jQuery across Internet Explorer, Safari, Chrome, Firefox and Opera.
04:44You can see that it works with all the current versions going back in some cases pretty far.
04:49So you can see jQuery works with IE 6 and later.
04:53But there are some known issues with some of the browsers and I have listed
04:56those here in the table.
04:57So you can see which browsers work well with jQuery and which ones don't.
05:01But the main point here is that all the main browsers that are being used today,
05:05this should cover the vast majority of all the browsers that your code is going to encounter.
05:10Okay, so now that we have seen what jQuery is, let's jump over the jQuery site
05:15and take a look at some of the features and see how to download and install it.
Collapse this transcript
Downloading and installing jQuery
00:00Okay, let's begin by taking a look at how we download and install jQuery onto
00:04our computer so we can use it.
00:06So, here I am on the jquery.com website, and I am going to go over here to the
00:11big Download jQuery button and click on it.
00:14Now, there are two different versions of jQuery.
00:17There is the compressed production version, and then there is the uncompressed
00:22development version.
00:24Essentially what you do is you usually download both of these and you use
00:27the Development version while you're doing your development, and then when
00:31you're ready to upload your finished project to your web server, you use
00:35compressed production version.
00:37And the reason you do that is because the compressed, production version is
00:41minified and compressed, and it's a lot faster to download to your user's
00:47computer than the uncompressed development version.
00:50The uncompressed development version is also a lot more readable, so if you're
00:53debugging, you can step through the code a lot easier.
00:56So, essentially what you do is you click on the link for the one you want to download.
01:00So for example, to download the compressed production one, I'll just
01:03simply click on this link.
01:05Now what might happen when you do this, is you might get a screen full of code
01:09like this depending on the browser that you are using, and the reason for this
01:12is because the browser doesn't recognize the .js file as something that needs to be downloaded.
01:17So, what we need to do in this case is let's just go back.
01:20In this case, what I am going to do is I am going to right-click on this link right here.
01:24I am going to choose Save as, and then I am going to save it as a JavaScript
01:29file, where I want to put the result.
01:33And in this case, I am going to put it into the Exercise Files folder for the course.
01:38You can see I have already got the uncompressed development version, and I am
01:42going to go ahead and save the minified version, which has this .min extension
01:47on it, and so I'll choose Save and it comes down.
01:51So, same thing for other browsers.
01:53Let's go ahead over to Firefox to see the same thing.
01:56Okay, so here we are in Firefox.
01:58I am going to click Download, and I am going to click on production.
02:01You can see the same thing happened here. What happened was Firefox doesn't
02:05recognize the .js as something to download.
02:07So again, same story. Just simply right-click or if you're on a Mac you can
02:12Ctrl+Click and you get the same experience. All right!
02:14So now that we have got jQuery downloaded, we can move on and start coding.
Collapse this transcript
Creating a simple jQuery-enabled page
00:01So, now that we have downloaded jQuery and saved it to our hard drive, we are
00:04going to create a simple jQuery enabled page.
00:07And for the first jQuery example, we are going to keep things pretty simple.
00:12So our example page is going to do three things.
00:14It's going to include a reference to the jQuery library that we just downloaded
00:19and then we are going to write an event handler and that event handler is going
00:21to respond to the page loading up in the browser window and it will display an
00:27alert that indicates the event handler was in fact called and jQuery is
00:32therefore working and doing its thing.
00:35So, before we do that, let's take a look at how you do this without jQuery.
00:39Typically, code that you want to have executed when the page is loaded is
00:43written something like what you see here.
00:46So what I have done is write a runOnLoad function and that displays an alert and
00:52you can see I am triggering that off the window.onload event.
00:57Now the problem with this is that the window's onload event only fires after
01:01all of the page content has been downloaded including any images that you have in the page.
01:07It's also a lot harder to add multiple independent onload functions because if
01:13you want to have more than one function run on the window onload event, you have
01:18to cram them all into this one function here or you have to use the DOM event
01:24model to do that, which is different among browsers.
01:26Now jQuery provides a way to run the code when the DOM of the page is actually
01:31ready, regardless of whether all of the content such as images has been
01:36downloaded, and that's called the document.ready event.
01:40It also makes it a lot easier to write modular independent on-load functions,
01:46and jQuery will just chain them together.
01:48So, let's see how to do this the jQuery way.
01:50To do this in jQuery, you write a document.ready event handler and it's written
01:55like the code you see here.
01:57So, this little dollar sign character, that's the jQuery object and you can
02:02write the dollar sign, you can write jQuery and call functions off of that.
02:06However, most of the jQuery code you'll probably see in practices written using
02:10this dollar sign character right here and that's where all of jQuery's
02:13functionality is accessed from.
02:15So what I am doing here is writing the jQuery object and calling the function
02:19with a parameter of document and then .ready, and I am passing it this anonymous
02:24function that will display an alert when the page is loaded.
02:29Now if you are not familiar with anonymous functions, I cover this subject in my
02:34course titled Practical and Effective JavaScript, which is also available here
02:38on lynda.com, so you might want to check out that title to learn a little bit
02:42more about anonymous functions.
02:44However, you can also just do this the old fashioned way.
02:47You can write an independent function, give it a name, and then pass the name
02:51of that function here.
02:52I am just doing it this way because it's a bit more succinct and concise to do it this way.
02:57So, this function calls an alert.
02:59It says hey, the page just loaded.
03:00So, this code will now execute when the DOM of the page has loaded and is ready
03:06for use rather than just waiting for all the page content to finish downloading.
03:11And as I said before, another advantage of this approach is that you can call
03:15this document.ready function multiple times with individual functions that
03:20you want executed and jQuery will just chain each one together to be called in succession.
03:25So that allows you to write much more modular code to run when the page gets
03:30loaded and they can be contained within separate JavaScript files and so on.
03:35So now that we've seen what we are going to do, let's jump over to the code and do that.
03:40If you have access to the exercise files, this is the folder they are contained
03:44in and you can see that what I have done is I have taken the jQuery file that we
03:49downloaded and placed it at the root of the exercise files because all of these
03:53guys are going to be using this file right here.
03:56This is that Visual Studio documentation file that I talked about earlier.
04:00You don't need to download this if you are not using it.
04:02I just happen to be using it so I put it at the same level as the jQuery file.
04:06So, let's go into the folder for this chapter and you'll see a couple of example files.
04:15This one here is called FirstjQueryPage_ start and that's the beginning of this lesson.
04:21If you'd like, you can go ahead and jump ahead to look at the finished version
04:24to see how the code looks and run it in your browser or you can just follow
04:27along with me as we edit the file.
04:30So, I am going to go ahead and jump over to my code editor.
04:34So, here we are in my editor and I happen to be using a free version of Visual
04:40Web Developer 2008 Express and you can freely download that from Microsoft if
04:45you are running on Windows, but there is a bunch of other tools you can use.
04:48It doesn't really matter which text editor you are using as long as you are
04:51comfortable with it.
04:52So, what I am going to do is insert a script reference to the jQuery library.
04:58So, I am going to go ahead and type script and the type is JavaScript and source
05:10is equal to, I am just going to type the little relative URL to back up to that
05:14jQuery library which is located up at the root level.
05:18So that's jquery-1.3.2.js.
05:27Now at the time I am recording this, 1.3.2 is the most current version but you
05:31might have a later version.
05:32So, that's what we do there.
05:34Okay, so now I have included the jQuery library.
05:36So now what I need to do is write the script code.
05:39So, writing right in the script tag down here and this one is going to be inline,
05:43so I am just gong to type, type= " text/JavaScript" and now I am going to
05:49write dollar sign, that's the jQuery object, and I am going call the document
05:55keyword .ready and I am going to pass that a function.
06:02Okay, and inside the function, I am going to say alert ("The page just loaded!").
06:15Okay, so now I am ready to preview this in the browser and again depending on
06:20the tool you are using, you know, this will be different for you or you can just
06:23go up to the file system and load the page up in the browser, but I am going to
06:26do it from right here. Choose View in Browser and you can see that the page
06:30comes up and sure enough the alert shows up, says "Hey, the page just loaded!"
06:34So, that's what happens in IE.
06:37Let's go back and take a look at what happens in Firefox.
06:39Okay, so here I am going to go to back up and choose Browse With, and we
06:44have got Firefox right there. Say Browse and you can see that there's Firefox
06:53and the same thing happens.
06:55Okay, so that's pretty much all you need to do to write your first
06:59jQuery enabled page.
07:01You can see that what we did was we included a script reference to the library
07:05and wrote a simple function that executes on page loads and now we are ready to
07:09get into detailed involved jQuery coding.
Collapse this transcript
Overview of jQuery's features
00:00Okay, so now we are ready to start diving deeper into jQuery and learning about
00:04all the great features of the library.
00:06But before we do that, let's take a quick look at the features of jQuery so we
00:12can get a sense of what's going to be coming up in the course.
00:15jQuery's features break down pretty much across eight major categories.
00:19There is the Core Functionality piece, and this piece implements pretty much the
00:23core jQuery functions, as well as some commonly used utility methods.
00:30Then there is the Selection and Traversal category, and this category includes
00:35functions for extracting content from documents and navigating among the
00:41contents of the document.
00:42This is a lot of what the DOM is usually used for in modern web development, and
00:47it's a very key piece of jQuery's power.
00:50You can think of this as the query in the jQuery name.
00:55Then there is the Manipulation and CSS category, and this category includes
00:59functions for editing and changing the contents of the document.
01:03So, you can think of this as the natural follow-on to the Selection and
01:07Traversal category where in that category you are finding content and in the
01:12Manipulation and CSS category you are taking that content and then
01:14manipulating it somehow.
01:16So, this category includes functions for editing and changing document content
01:20and working with CSS data such as positioning information and so on.
01:26That's followed by the Events category, and this simplifies working with the
01:30modern DOM events and provides a lot of common Event Helper functions that
01:35cover some common scenarios that pop up in interaction design in web pages.
01:41Then there is the Effects category, and this is where things begin to get interesting.
01:45This category contains functions for creating some basic animations and effects,
01:49such as hiding and showing elements, moving things around, fading things in and
01:53out, that kind of stuff.
01:55Then there is the AJAX category, and the AJAX category includes utilities for
01:59working with AJAX, such as loading content from remote pages, dealing with
02:03JavaScript object notation data, and so on.
Collapse this transcript
2. Retrieving Page Content
Overview of selectors and filters
00:01In this section, we are going to look at how to use jQuery to retrieve
00:05content from the page.
00:07In fact, you could use jQuery to do almost nothing else more than this.
00:11This is one of jQuery's best features.
00:13This is something that you would usually use the DOM for in the past and it's
00:16one of the things that jQuery makes much easier.
00:19We are going to start by looking at the selectors and filters feature of jQuery.
00:26jQuery's selectors and filters do essentially exactly what their name
00:32implies that they do.
00:33They select content from the document, so that it can be manipulated using other
00:39functions, either jQuery functions or native built-in JavaScript functions.
00:44And you can think of jQuery selectors and filters as the query part of the jQuery.
00:49Now, the way these work is that jQuery selectors will return an array of
00:55objects that match the selection criteria that you give them and the filters
01:02will operate on a selector to further refine the results array that the selector returns.
01:09So essentially, you use a selector and that will create an array of objects or
01:15elements that are in the document and thefilters will then further refine them.
01:19Now, it's important to know that this array that comes back is not a set of DOM elements.
01:25It's a collection of jQuery objects that are wrapped around the DOM elements and
01:30these jQuery objects provide a large number of predefined functions and
01:35properties for further operating on the objects.
01:39Now, you can get access to the underlying DOM element for each one of these
01:44objects, if you want to, but the whole purpose of doing this is that you are
01:48wrapping them in jQuery objects, so that you have a whole bunch of convenience
01:52functions and high-level ways of operating on them without having to resort to
01:58the DOM in order to do that.
01:59So, now that we understand what selectors and filters are, let's take a look at
02:04how to use some of the basic selectors and filters in jQuery.
Collapse this transcript
Using basic jQuery selectors
00:01Basic jQuery selectors are based on the familiar CSS syntax that you are
00:07probably already familiar with, and they work pretty much the same way that CSS does.
00:12I have listed here the basic CSS selectors in the table and you can see they
00:18correspond directly to their CSS counterparts.
00:22So, for example, if I pass a tag name to the jQuery selector, it will find me
00:29all elements that have that same tag name.
00:32Similarly, if I pass in an identifier, jQuery will find the element that has that id.
00:40The same thing goes for class name.
00:41If I pass in a class to the jQuery selector, it will find all the elements that
00:45have a class attribute that have that class name.
00:49And just like CSS, you can get a little bit more fancy.
00:52You can look for tags that have a particular class name, or you can look for a tag
00:57that has a specific id and has a class name on it.
01:01And then, there is a wildcard character, the special character, the asterisk,
01:04basically means find all of the elements on the page.
01:09And if that sometimes happens, you need to iterate over all of the elements in the page.
01:14So, let's take a look at how you would use jQuery versus using the plain browser
01:21DOM to get at information in a document.
01:25So, here I have a pretty fairly simple HTML file and it's been stripped down a
01:31bit for purposes of illustration.
01:33But you can see it has an unordered list in it and some list items and
01:37paragraph tags and so on.
01:38So, let's take a look at some comparisons of the DOM and jQuery.
01:42If I want to get all the paragraph tags in the document, using the DOM, I would
01:46do something like this.
01:47I would use the document. getElementsByTagName and pass in the p, and that would
01:52get me all these paragraph tags.
01:54To do the same thing using jQuery, you can see all I have to do is call the jQuery object.
01:59That's the little $ sign right there.
02:01That's the global jQuery function, and I simply pass in the parameter p, and
02:05that will come back with what is called a matched set, or a wrap set it's
02:10sometimes called, or matched elements.
02:13It comes back with a list of all the tags that match that particular
02:17parameter right there.
02:18Okay, suppose I wanted to get the tag that had the id "list1" and again using
02:23the DOM, I would use the standard DOM function getElementById, and I would
02:28give it the id that's this list right here.
02:30That's what the DOM would come back with.
02:32To do the same thing in jQuery, you can see I am just using standard CSS syntax,
02:37a little hash mark with the id in front of it, and that comes back with the
02:42element for that id.
02:45Okay, let's get a little more fancy.
02:46Suppose you want to get all the list item tags that have class a on them.
02:50Well, to do that using the DOM, it's a little bit more complicated.
02:53You would have to first use the getElementsByTagName function right to get all
02:57the <li> tags, then you would have to write some for loop that would loop
03:01through all the results in that array and get the class attribute and compare it
03:07to this name right here, a, to see if it should be included in the result side.
03:11Well, jQuery does that for you and again, using CSS syntax, all I have to do is
03:16call the jQuery object with this expression right here, list items that have a
03:20on them, and it accomplishes the same thing.
03:23And finally, let's imagine you wanted to have a situation where you wanted all
03:27the tags regardless of what kind of tag they were that have a class b on them
03:32but only if they are inside a <ul> tag.
03:36Again, using DOM this would be pretty complicated.
03:38You have to get all the tags, see if they have the b class, see if they are
03:42inside the ul, that kind of thing, but jQuery makes this really easy, again,
03:46using pretty standard CSS syntax with the descendant inside the ancestor.
03:50So, this basically says get me everything that has b class as long as
03:53it's inside the ul.
03:54That would come back with these list items right here.
03:57In addition to the basic selectors, there are some selectors that are a
04:01little bit more advanced.
04:02These are the hierarchy and combination selectors.
04:06So, these allow you to get a little bit more advanced in selecting page content.
04:10It basically allows you to select elements based on hierarchical relationships
04:14or a combination of criteria.
04:17So, the first two do just that, whereas in jQuery, usually you will see
04:22one selector passed in.
04:23If I passed in a comma-delimited list like p, div, image, it would find all of
04:31the elements that match everything in the list and for class 1 and class 2,
04:35this means find all the elements that have both class 1 and class 2 on them.
04:39And they both have to be there.
04:41The next couple of examples illustrate hierarchical relationships.
04:46So, if you wanted to find all the child elements that are direct children of a
04:49certain parent, you would use this expression right here, whereas if all you
04:53cared about was finding a tag that appeared anywhere underneath a given
04:57ancestor, you would use this form, with the ancestor and then a space, and then
05:02whatever you are interested in here.
05:04In the next two, you have to deal with siblings.
05:06So, the next operator finds whatever element is next to this element, right,
05:13the previous element, and the siblings element says hey!
05:17Get me all the sibling elements that come after the previous element right here,
05:21and match the selector that I am passing in for siblings.
05:26Okay, so let's take a look at some of these in action.
05:29We will switch over to code.
05:32Okay, so if you look inside your exercise files, I have got a file right here
05:35named Basicselector_start and I am going to start off there.
05:38So I am going to open this up in my editor and a couple of things we are going to do.
05:43So, you see that this is pretty much the document we are working with.
05:46Very similar to the one in the slides.
05:49Got an unordered list that's in paragraphs.
05:52Let's go back to the source.
05:53So, what I am going to do is write a function that executes when the page loads
05:59and I am going to do that on the ("document").ready event.
06:05Okay, so here is what we are going to do. .ready. That's a function.
06:10Let's try a couple of examples.
06:11So, I will say get me the <p> tags.
06:14Now, don't pay attention too much to this.
06:16What I am about to do.
06:17I want you to focus on the selectors right here.
06:19We will get to this stuff later.
06:21I just need to make the result visible, so you can see them on the screen.
06:24So, just bear with me while I do this.
06:26I am just going to make the CSS border a 3 pixel solid red line, so that we
06:30can see everything.
06:32What this is going to do is select all the paragraph tags.
06:35So, let's try that out in the browser.
06:37And as you can see, all the paragraph tags are now inside bold red outlines.
06:42Let's go back to the code and try something else.
06:45Okay, let's do the same thing, only this time, we are going to do it for
06:49everything that has a class of a. So you can see that these list items, that
06:54paragraph, those should all be inside red borders. Let's try this.
06:59I am going to refresh, and you can see sure enough, that's the case.
07:03All right, now let's go back and try the id selector, back to the code.
07:07Okay, so now I am going to put in list1 and that's this unordered list right here.
07:12So, now the list itself should have a border around it.
07:15Okay, let's refresh and sure enough that works.
07:18And then finally, let's one more.
07:21I am going to try all paragraphs that have class b assigned.
07:24All right, let's refresh and you can see that works just fine.
07:30Okay, so let's try some hierarchical ones now.
07:33Here we are in the system again and I am going to do the same thing.
07:37There is a file here called HierCombo.
07:39I am going to open that in my editor.
07:42Again, if you follow along with me, we are going to try some
07:44hierarchical relationships now.
07:46What I am going to do first is try get me paragraphs and list items that have b
07:54applied, and apply that CSS trick, so we can see it.
08:02Okay, and let's bring this up in the browser.
08:05Okay, and you can see that that works, all the paragraphs and everything with the b.
08:09All right, let's try the descendant operator.
08:12And the descendant operator basically will ask for things inside an unordered list,
08:17and list items with an a on them.
08:20I will just try that.
08:21Okay, we will refresh, and you can see that that worked.
08:26All right, now let's try the next operator.
08:30So, we are going to say get me the paragraph that's next to the unordered list.
08:36Refresh, and you can see that the first paragraph that was next to the list is now bordered.
08:41Okay, let's try the siblings one.
08:42For this one, I am going to say, let's see. How about whatever it is, let me
08:48get the list1 and get me the paragraph siblings of list1.
08:54So, all these paragraphs are siblings of list1.
08:56Let's try that and sure enough it works.
08:59Okay, so now you have seen how to use basic jQuery selectors. Let's move on now to Filters.
Collapse this transcript
Using basic jQuery filters
00:01So as you have seen, jQuery selectors are very powerful utilities for selecting
00:07content inside web pages.
00:09So now we are going to look at a feature of jQuery that make selectors even more
00:13powerful while retaining the CSS style simplicity that goes along with the
00:20selectors we have already seen.
00:21And these are called filters.
00:24Filters work in conjunction with selectors and they provide even more
00:29fine-grained control over how elements are selected in the document.
00:34jQuery filters fall into six different categories.
00:39The first category is Basic and these provide basic filtering like getting
00:43the first or the last or the even or odd numbered items in the returned set from a selector.
00:49The next is content based filtering.
00:52Content filters take a set of elements and they filter out elements based on their content.
00:58Like for example, whether an element contains a particular string.
01:03Then there is Visibility filters and these filters act on elements using
01:07their visibility setting.
01:09And they will filter out elements that are either hidden or visible.
01:13Next comes Attribute filters.
01:15Attribute filters will examine a given attribute on an element and they will use
01:21that attributes' value to determine if it should be filtered out or included in
01:26the selector's result set.
01:28Then there is Child filters, and Child filters select elements based upon their
01:34relationship with their parent element.
01:37Then finally, there is the special set of filters that work on form elements and
01:43you can use these as really convenient powerful ways of processing elements in
01:48forms based upon what kind of form fields they are, whether they are enabled or not,
01:53whether they are checked or not, and that kind of thing.
01:55Okay, so let's take a look at the Basic jQuery filters.
02:01So Basic filters allow you to refine the results of a jQuery selector by only
02:07including elements that match certain conditions.
02:12If you're taking a look down this list and you are familiar with CSS, you will
02:15probably notice that again these match up very closely with CSS syntax.
02:20So for example, the :first filter selects only the first instance of the
02:26selector's returned set.
02:28Similarly, last, it will select only the last instance.
02:31Then there is even and odd which select only the even numbered elements or the
02:36odd numbered elements in the returned set that the selector came back with.
02:40Then there is a few more down here that operate on position.
02:43So the eq filter will take an index and it will filter out elements that are not
02:49positioned at that index.
02:50So it will only include the element at that index in the result set.
02:53Then there is a greater than and less than, and they do what you might think.
02:57For example, the greater than operator will only include elements that are past
03:02this given index whereas the less than filter will only include elements that
03:07are before that index.
03:09The header filter will select all header elements in the page, H1, H2 for
03:16example all the way up to H6.
03:19The animated filter will select all the elements that are currently being
03:23animated by a jQuery in some way, and you would use this in conjunction with the
03:29animation ability of jQuery, which we will get to later.
03:32Then finally, there is a negation filter and it's called the not filer.
03:37The not filter takes a selector and that will include elements in the result set
03:41that don't match the given selector.
03:45So we have reached the point now where we can jump over to the code and see
03:48how some of these work.
03:49So let's fire up our editor and see how we can get even more power out of jQuery
03:54selectors by adding filters.
03:55If you look in the exercise_files folder, you can see there is a file in here
04:01named BasicFilters and there is a start and a finished version.
04:05Now I am going to use the start version to work towards the goal.
04:08If you want to follow along with me, you can or you can just go ahead and jump
04:11ahead to the finished version to see the results for yourself.
04:14So here we are in the code.
04:16I am going to go back and show you the Design view.
04:20So this is the same document that we were using in the previous example, only
04:24now we are going to use filters as well.
04:26Okay, so let's jump right in and try some things out.
04:29So I am going to write a jQuery expression.
04:32Now I remember last time I started off by just getting p tags and then I
04:37added a little css trick.
04:39Again, I don't want you to focus too much on this css right now, because I am
04:42just doing this to make the results visible.
04:44We will get to jQuery's css handling later on in the course.
04:49So for now I am just doing this to make it possible to see the results.
04:53So instead of just getting p tags, however, let's try some filters.
04:56So I am going to write p:first.
05:00So what this will do is find the first paragraph tag and put a border around it.
05:06So let's see if that works.
05:07I am going to save, and let's bring this up in the browser.
05:11And you can see that it worked.
05:13So the first paragraph now has a border around it.
05:15Okay, let's go back to the code.
05:18Let's try getting a little more fancy.
05:20So I will copy that and paste.
05:24So let's try the last operator.
05:27And as you might expect the last operator should do pretty much the same thing.
05:31Let's go to the browser, let's refresh and now you can see that the last
05:35paragraph is selected.
05:38Let's get a little more fancy.
05:39Let's go back to the code, comment this guy out there.
05:41Now we are going to do the even numbered paragraphs.
05:46So let's save and go back to the browser and I am going to refresh.
05:51And now you can see that the even numbered, and this may not look even
05:54numbered because that's the number one, but remember jQuery is starting off
05:58indexing at index 0.
06:00So this is 0, this is 1, this is 2, and this is 3, even though I have got them
06:04labeled starting at 1, 2, 3, and 4.
06:07In programming parlance the 0 based index is first and that's obviously an even number.
06:12Okay, let's go back to the code and let's just try odd just to show that it works.
06:17Okay, and let's refresh and now you can see the odd ones are highlighted.
06:23So that works obviously with tags. Let's try it with some classes.
06:29Now I am going to comment that line.
06:30Let's try getting the first instance of whatever has the a class applied to it.
06:37You can see that it's going to be this list item right here.
06:40So let's try that out, okay, we refresh.
06:44And sure enough, that first time with the a class is selected.
06:48Let's go back to the code.
06:50Let's try every even numbered thing that has b assigned to it.
06:57So let's look for the class b and even.
07:01So we save and let's refresh.
07:04And you can see that this is item number 0 for b, this is the second b item right there.
07:10So those are both being highlighted.
07:12Let's comment this.
07:16Now let's try looking for paragraph tags, but only paragraph tags that are
07:20greater than index 1.
07:24So this is going to be index 0; this is going to be index 1.
07:28So we should get 2 and 3 here included.
07:31So let's save, go to the browser and refresh.
07:36And sure enough that's exactly what happened. Only indexes 2 and 3 got highlighted.
07:41All right, finally, let's try the not operator.
07:44Okay, so here we are back into the code.
07:46This time let's try looking for paragraphs that are paragraphs not equal to index 2.
07:57So it's going to find the paragraph at index number 2 and it's going to select
08:01all the paragraphs that are not that one.
08:03So they should all have a nice red border around them.
08:05So what that means basically is-- let's 0, 1, 2.
08:10So this guy will be excluded, but the other three should have a border.
08:14So we will save and we will go back to the browser and let's refresh.
08:18And you can see results are exactly what we expected.
08:22This one here is not bordered and the other three are.
08:26Okay, so that pretty much wraps it up for basic jQuery filters.
08:30Now you know how to combine filters and selectors to do some pretty powerful
08:33selecting techniques.
Collapse this transcript
Using jQuery attribute filters
00:01 Now that we have taken a look at some basic jQuery selectors and filters,
00:04 let's get a little more advanced.
00:06 We are going to look now at Attribute filters.
00:09 And Attribute filters provide the ability to further filter out the results of
00:14 a selector statement based upon attributes that are on the elements being selected.
00:21 You can see here I have listed a table of the available filter attributes.
00:25 So let's just go down the list.
00:27 The first one where you put the name of the attribute inside these braces
00:30 basically only includes the element in the result set if that attribute exists
00:36 on the element. Doesn't matter what value it is;
00:38 it just has to be there.
00:39 The next one checks to see if the attributes' value is equal to a value that you supply.
00:46 And then from there, the next one only includes the element in the result if the
00:51 attribute does not have the value that is listed here.
00:54 The next two are very similar to regular expressions in JavaScript.
00:58 This one checks to see if the attribute starts with the string that you give
01:03 here and this one, the $=, checks to see if the element ends with the value
01:08 that you have given.
01:09 The next one here, the *=, that checks to see if the attribute contains the
01:15 string that you specify anywhere within the attribute value.
01:19 Then the last one is kind of like a compound statement.
01:22 It only includes elements that match all of the specified Attribute filters.
01:27 So you can put 1, 2, 3 as many as you want on the filter statement.
01:32 One of these filter expressions will take one of these forms.
01:36 And you can specify as many as you like, and then only elements that match all
01:40 of those guys will be included in the result set.
01:44 So let's exercise some of these.
01:46 Let's go over to the code and try some out.
01:50 Let's begin by just taking a quick look at the document.
01:54 The document is a little bit different than the last time around.
01:56 I have added a couple of more attributes to this paragraph here on the end and
02:00 that's pretty much about the only change that I have made, so that we can test
02:04 out the multiple Attribute selector.
02:06 So, okay, let's get started.
02:08 I am going to write a jQuery selector statement and that's going to look like this.
02:12 And I am going to say, hey, let's get all the p elements like we have done in the past.
02:18 And again, just like we have done in the past, I am going to put a nice bold red
02:24 border around the results so that we can see visually what's happening.
02:31 For this particular selector I am going to specify that I only want to select
02:36 paragraphs that have a class attribute.
02:38 So I put the word class inside the braces and you can see down in here only two
02:42 paragraphs match that.
02:43 There is this one here and this one.
02:45 So only those two paragraphs should have this border applied to them.
02:51 Again, we will get to this part later.
02:53 Right now I will just focus on this part right here.
02:55 So I am going to go over to the browser. Let's see.
02:59 Okay, I am going to bring up that file and you can see that the paragraphs 1 and 3,
03:05 the ones that have the class attribute are in fact bold red border around.
03:10 So let's go back to the code and make a couple of minor changes.
03:14 Okay, so here we are back in the code.
03:16 Let's try something a little different now.
03:18 First, I am going to copy this and then comment it out.
03:21 And then I am going to paste that guy back in.
03:23 Now what I am going to do however is look for a paragraph, but I am only going
03:27 to filter on paragraphs that have an id attribute that is equal to para 1.
03:34 And you can see that should be this guy right here. That's the id.
03:37 So now only that paragraph should have the border applied.
03:41 Let's go back to the browser and I will refresh.
03:43 Yeah, and sure enough that worked just fine.
03:46 All right, let's continue on.
03:50 We are going to go back to the code.
03:51 Now we are going to get a little bit fancy.
03:53 I am going to comment that guy out again.
03:55 Let's try one of the Attribute filters that does the start with.
04:00 So now we are going to say let's find paragraphs that have the id attribute but it has to start with the string "para."
04:09 And you will see that there is two of those, right.
04:10 There is this one here, para 1, and then there is para 4.
04:14 So now only these two guys should be selected by this selector and filter
04:19 combination and have the border applied.
04:21 So let's go back to the browser.
04:24 Okay, now let's refresh and sure enough that's exactly what happens. So far, so good.
04:29 Okay, let's go back to the code and we will try one final example.
04:34 So for our final example, let's try a multiple attribute requirement.
04:40 So what we are going to do now is say we're looking for paragraphs that have ids that start with para
04:49 and it also has to have a language attribute that contains, which is the *= operator, en-, and you can look
05:02 down here you will see that the only that's going to match that is this guy here,
05:05 because he starts with para there.
05:08 And he has got language attribute that contains English for US, but that's the
05:13 string we are looking for.
05:14 Okay, let's go ahead and save and we will go back to the browser and only that
05:19 guy should now be highlighted.
05:21 So let's see, okay, and I will refresh.
05:24 And as you can see that worked.
05:27 Let's just try breaking that expression to make sure that it in fact does not
05:31 work when it shouldn't.
05:32 So let's go back to the code.
05:33 What am I going to do is just change this statement to say it has to start
05:38 with something like ben.
05:40 So now if it doesn't contain that, it should break.
05:43 And we will go back to the browser and now nothing should be highlighted.
05:45 We will refresh and sure enough that works.
05:49 So that's how Attribute filters can be used to further refine selection in the document.
05:57 So now we have seen how to select elements by filtering out their attribute values.
06:03 So let's move on now to our next filtering group.
06:07
Collapse this transcript
Child, visibility, and content filters
00:00 We are going to look now at Content and Visibility filters as well as Child filters.
00:08 Content and Visibility filters examine the content of the elements that are
00:13 returned by the selector expression or their visibility property to determine if
00:18 they should be included or excluded from the result set.
00:22 So the Content filter has four options.
00:25 There is the contains filter, and you pass it some text.
00:29 What will happen here is jQuery will look inside the child elements of the
00:35 element on the selector to see if they contain this text string anywhere within
00:40 their content, in any of their child elements.
00:43 The next one, the empty filter, says only include elements in the result set
00:48 that have no child elements inside them.
00:50 The next one, the has filter, you pass that a selector and this will filter
00:56 elements in the result set that contain at least one element that matches this
01:03 selector that you have given it.
01:05 Finally, there is a parent content filter that says only match elements in the
01:10 result set that are themselves parents.
01:12 For example, they contain at least one other elements including text elements.
01:19 For the Visibility filter there are only two options.
01:21 There is visible and there is hidden, and that's pretty self-explanatory.
01:24 This only includes elements that are visible; this one only includes
01:27 elements that are hidden.
01:28 Let's take a look at the Child filters.
01:31 Child filters allow you to refine the selection by looking at the relationship
01:35 that the element has with its parent elements.
01:38 There are four options here. Actually there are more, because the
01:42 nth-child option has a few.
01:44 So the nth-child filter has a couple of different things you can do.
01:48 You can give it even or odd.
01:49 It kind of works the same way as the even or odd filters that we looked at
01:53 in the previous example.
01:54 Only in this case it's operating on child elements, not the parent elements.
01:58 So this will only match elements that are even or odd inside the parent.
02:05 The nth-child, where you are given an index, will match all elements that are at
02:09 that index inside the parent.
02:11 Then the last one, which is pretty powerful, is the equation.
02:15 You can actually give nth-child an equation of the form xn+m and m is option obviously.
02:20 For example, you can say 2n for every second one or you can say 3n+1 and then n
02:28 will start counting at 0.
02:29 It will count 0, 1, 2 and so on.
02:32 Then this equation will be calculated and then that will be the index of the
02:36 element that gets selected. We will see an example of that in a moment.
02:39 Then finally, there are a few other options.
02:41 There is first-child, which will match elements who are the first child of
02:44 their parent, and then predictably there is last and only and those will match
02:48 elements who are the last child of their parent or the only child of their parent.
02:53 Back in the folder for the examples, I am going to start off with the
02:57 ChildVisCont_start file, and here is the finished example if you want to jump ahead.
03:01 So I am going to open this up with my text editor.
03:04 This is the example file at we are going to be using for the Child and
03:08 Visibility and Content filters.
03:11 So let's go ahead and get started playing around with some of the examples.
03:15 Let's try something pretty simple. I am going to write a query that says get
03:21 me paragraph tags, only paragraphs that contain the text string 3.
03:30 Again we are going to draw our little CSS trick to make it visible and we will
03:36 give it a border that's 3 pixels solid red.
03:42 So let's save that and let's bring that up in the browser.
03:47 When you look very quickly, you will see that there is a text string 3 right
03:50 there for this guy, so that's the one that should be selected, because where
03:53 paragraphs that contain 3.
03:56 Save, and we are going to bring up it in the browser and you can see that that's
04:00 the guy that got highlighted.
04:02 All right, let's go back to the code and make a quick change and watch what happens.
04:07 First, that guy was misnumbered.
04:09 Now let's take off the p. Let's just do this.
04:11 Let's just do contains by itself.
04:13 So what this is going to say is I don't care what element it is.
04:16 Just select all the elements that contain the text string 3.
04:21 Now at first blush you might say okay.
04:23 Well, that's going to be this guy here, right, the list item and it's going
04:27 to be this paragraph.
04:28 So let's watch what happens when we go out to the browser.
04:30 I am going to save.
04:32 Now I am going to refresh. Wow!
04:35 Everything got selected.
04:36 So the list item here that has the 3 in it and the ul right, then it
04:42 looks like the entire container here, the body.
04:45 And here the paragraph has a 3.
04:46 So let's go figure out what's happening.
04:49 So here we are back in the code.
04:51 Remember what we are asking jQuery now to do is say, hey, get all the elements
04:56 that contain the text string 3.
04:58 Well, sure, the list item contains the text string 3, but so does the ul. Why?
05:03 Because the ul contains the li and that contains that string.
05:07 Well, so is the body, and in fact you can see here that's the reason why the
05:10 whole body is being outlined is because even though this paragraph right here
05:14 has the string in it.
05:15 Well, so does its parent and its parent is the body tag, and the parent of the
05:19 body is the HTML. Everything is being highlighted.
05:22 So this is an example of where results may be a little unexpected, if you are
05:26 not keeping in line to the hierarchical relationship that jQuery looks at when
05:30 it's examining tags.
05:32 So to limit what happens when you are using filters like Contains or so on,
05:36 you might want to put the scope in there that you are looking at.
05:39 So for example, if you only wanted the list item in the paragraph, we can say
05:43 li:contains(3),p:contains(3), then that will do the right thing.
05:50 Let's move on to the next example.
05:52 So I will comment that guy, and copy and paste and uncomment.
05:59 Now let's try the parent operator.
06:02 So we are going to get paragraphs that are parents.
06:07 So this will look at all the paragraph tags and if they contain another tag,
06:11 then including text items, they will be included in the result set.
06:15 So I am going to save and then refresh, and you can see that all the paragraphs
06:20 got selected because they all contained content.
06:23 So they were all bordered.
06:25 Now let's go back to the code.
06:28 Let's try another example.
06:30 Comment that guy, put this one in.
06:33 Now let's try an example of the has operator.
06:36 So let's get rid of this.
06:38 We are going to ask jQuery to find us unordered lists and we are only going to
06:43 look for unordered lists that has a list item in them that has a class
06:52 attribute that's equal to a.
06:55 So what we are doing here is this is selector all on its own, so list items that
07:00 have a class equal to a, but we are not selecting the list items, what we are
07:06 selecting is the ul.
07:08 But we are only selecting the ul, if it has a list item inside that matches this.
07:13 So let's save and go back to the browser.
07:18 So now we are going to refresh, and you can see that the ul was in fact included
07:24 in the results, because it does have a list item that has that class.
07:27 So if we go back to the code, and we change that to class=c. Well, there is no
07:31 class c. So now this will result in nothing being highlighted.
07:35 So we will save, go back to the browser.
07:37 Now we will refresh and you will see that the unordered list is not
07:40 highlighted anymore.
07:42 Let's do a couple of nth-child examples.
07:44 So let's go back to the code and let's comment this and paste.
07:50 So now we are going to do this.
07:51 Let's say we are going to look for the, unordered list and we are going to look
07:56 for the list item that's the nth child = 3.
08:06 So this says get me the list item that is the third child that's inside an unordered list.
08:14 Let's go back to the browser, and refresh, and you will see that sure enough
08:19 the third child is highlighted, because that's the third list item that's inside the ul.
08:25 Let's go back and try something else.
08:27 Let's just try now.
08:28 Let's just say li:last-child and we will save and we will refresh.
08:35 Let's bring it up and sure enough, the last list item child is selected.
08:41 Finally, let's try an equation.
08:44 Let's comment this guy out and we will do this.
08:47 Let's try, ul li:nth-child, but in this case let's do (2n).
09:00 So now what's going to happen is n is going to start out counting and it's going
09:04 to select every other one.
09:06 So let's refresh in the browser and you can see that items 1 and 3 were selected.
09:13 That's something worth noting.
09:14 So let's go back to the code.
09:16 Earlier, you might have caught it when I said that jQuery operates on elements
09:20 that are 0 index based.
09:23 This is an exception here.
09:24 When you are using the nth-child operator and you are using the equation,
09:28 the n variable starts off counting at 1.
09:30 It doesn't start off counting at 0.
09:32 So that's something to keep in mind.
09:34 But you can see that it has selected pretty much every other one in the bunch.
09:39 So if I changed this to 3n, and when you refresh you can see that it's 1, 2, and 3.
09:47 So 3 time 3n is 3.
09:49 That's the one that got selected.
09:51 So that's how Child and Content Visibility filters work.
09:56 Let's move on now to our next lesson.
09:59
Collapse this transcript
Form selectors and filters
00:01In addition to providing selectors that work across generic elements of a
00:05document, jQuery also has a set of selectors and filters that are just used for
00:10dealing with form elements.
00:12They work pretty much like any other selector, but they happen to start with a
00:16colon character, like filters do.
00:19These kinds of selectors and filters make it really easy to build form
00:23processing logic using jQuery.
00:26So you can see the table here.
00:28This table lists the available selectors for form elements, so they pretty much
00:33match up directly with the names of the various form elements that you will
00:39find in a document.
00:40So for example, you have selectors for input, and text, and password, and radio,
00:44and checkbox, submit, all the way down.
00:46You can use these to select those individual kinds of elements on the form.
00:51So in addition to selectors, there are form filters, and these are the form
00:56filters that are available.
00:58So there is only a few of these, and they are essentially convenience filters to
01:04help you select form elements that are in certain state.
01:07So for example, we have a filter for enabled and disabled, and we have got a
01:11filter for check and selected.
01:13So for example, the checked one matches form elements that are checked, and
01:17obviously it applies only to radio buttons and check boxes, whereas the enabled
01:21and disabled will match form elements that match that state.
01:25So let's go ahead and fire up our code editor and take a look at a couple of form examples.
01:31So I am going to jump over to the code here.
01:33I am going to go ahead and open up the Formselectors_start file, and that's in
01:39the Chapter 2 section of the exercise files.
01:43Now if you've got access to the exercise files, you can open this up in your own
01:47text editor or just follow along with me watching the movie, or you can go ahead
01:51and skip ahead to the _finished version to see how everything works.
01:56So I'm going to go ahead and open this in my editor.
01:59So this example is a little bit different than the other examples we've looked at so far,
02:03so let's begin by looking at the Design view. And you can see what we have is a
02:09source file with several form fields in it.
02:11We've got some input fields, we've got some radio buttons, check boxes, and some other things.
02:16So let's go back to the source and take a look at how we use jQuery to select
02:23each of the form elements.
02:24Okay, so up here in my document.ready function I'm going to start by writing my query.
02:29And I'm going to do things a little bit differently than I did in my previous examples.
02:34In my previous examples, I was surrounding elements with a red border box, but
02:38not every browser supports doing that with form elements, so I'm going to do
02:41something a little bit different.
02:43Let's begin by selecting form elements that are input fields.
02:48So I'm going to type form: input.
02:52And then to show you, visually, that the selector is working, I'm going to type
02:56"before" and this is going to put some text before the selected element.
03:03So instead of doing the red border, I'm going to do this. Okay.
03:06So what this is going to do is select all the input elements of the form and
03:10then put the word "INPUT" in front of the form element. So let's save.
03:15Let's go over to the browser and refresh.
03:17Okay, so I'm going to refresh this in the form.
03:20Okay, so you can see that when I refresh the document, the word "INPUT" got put in
03:24front of every form element that is an input element.
03:29And since all of these elements are input elements, you can see that the radio
03:32boxes and the check boxes and the text fields and everything had the word
03:36"INPUT" put in front of it.
03:38Okay, so let's get a little bit more specialized.
03:41Let's go back to the code.
03:43So, back here in the code, let's try something else.
03:45Let's comment this out, and then we'll copy and paste.
03:49What we're going to do this time is select text elements.
03:54So in this case, we're going to change this to "text" and we're going to put the
03:58word "TEXT" in front of all the text elements.
04:02So I'm going to save, and we're going to go back to the browser.
04:05And now when I refresh you can see that only the text elements have been
04:08selected by that jQuery selector.
04:10So none of the other elements now have text in front of them--only these two guys do.
04:15And let's go over to the Firefox browser, so I can show you it working there.
04:19Okay, here is the same document.
04:21And I refresh, and you can see that the word "TEXT" got inserted based on the
04:25query we wrote in front of the text elements.
04:28Now let's go back to the source.
04:30What I'm going to do now is comment this guy out.
04:34And I'm going to switch over to Design view, and I'm going to insert a row
04:39in front of this guy.
04:41Now you can do using whatever HTML or text editor you're using.
04:44What I'm doing is inserting a table row, and I'm going to put in a Disabled element.
04:51And I'm going to put in a text field, using my toolbox.
04:54Again, you can use whatever favorite HTML that you're using.
04:59Okay, so now I'm going to make this guy disabled.
05:02So we'll go to the source and we'll type "disabled=disabled."
05:09Okay, so he's disabled now, and what we're going to do is write another query
05:15that shows how to select elements that are enabled.
05:19So let's copy and paste there. We'll uncomment.
05:23So what we're going to do now is just do form and text, but we're going to put
05:28the enabled filter on it.
05:30And so I'm going to change this to read "ENABLED." I'm going to save.
05:37So now, what this will do is put the word "ENABLED" in front of text fields, but
05:41only the enabled ones.
05:42So let's switch back to the browser.
05:44Okay, and I'm going to refresh.
05:46And you can see that even though I've got three text fields, only the enabled
05:50ones have the word "ENABLED" in front of them;
05:53this disabled guy does not have text in front of it.
05:56Okay, so let's take a look at Firefox.
05:58Okay, here's the Firefox version.
06:01And I refresh and you can see that it's working there as well.
06:03Let's go back to the source.
06:06So if I were to change this to disabled then the reverse would happen;
06:09only the disabled field would have the text in front of it.
06:12Okay, so let's comment this.
06:14Okay, so for the next example what we're going to do is show how to use jQuery
06:18to select items that are checked.
06:21So let's go back to Design view.
06:23And we're going to have some items that are checked by default.
06:26So what we're going to do is check this radio button and this check box and this check box.
06:32So let's go into the source and do that.
06:35I'm going to--let's say this guy equals checked. And we'll check this first
06:43checkbox as well, and we'll copy that and put it on this one as well.
06:51So let's go back to the Design view.
06:53So you can see now that several of the elements are now checked: this radio
06:58button and these check boxes.
07:00Let's go back to the jQuery code now, and let's go to the top.
07:04What we're going to do now is write a selector that selects form elements that are checked.
07:12And I'm going to use just the filter by itself here, and I'm going to write
07:16"before" and I'm going to insert some text that says "CHECKED." Okay.
07:24So let's save.
07:25Let's go back to the browser and see what happens.
07:27Okay, here were are, in the browser. I'm going to refresh.
07:29And you can see that only elements that are checked, including the radio button
07:34and these check boxes, have the word "CHECKED" put in front of them, whereas
07:39these unchecked items, these two check boxes and this radio button, do not.
07:43So that's pretty useful, and you can see how jQuery can be pretty powerful for
07:47processing form elements, selecting things that are checked and enabled, and so on.
07:52Let's do one more example.
07:54What we're going to do now is only select check boxes that are checked, so we're
07:58going to write a short query that ignores the radio button.
08:01Okay, let's go back to the source.
08:03Okay, so here we are, in the source, and I'm going to comment this guy out and
08:07do a copy and a paste.
08:13And I'm going to save.
08:16So now I'm going to writing in front of the checked filter just ":checkbox".
08:22What this is going to do now is look inside our form for all the check boxes
08:26that have "checked." And I'm going to save.
08:29Okay, let's go out to the browser again.
08:32So now when I refresh, you can see that the word "checked" does not appear in
08:36front of the radio button, only the check boxes.
08:39And I could the code to go back and, instead of writing checkbox, I could
08:42write radio button.
08:43Let's go back over to Firefox and see it there.
08:46Okay, so I'm going to refresh, and you can see it's working here as well.
08:51Okay, so that's how you use jQuery to process form elements.
08:56You can see that this is a very powerful feature.
08:59It makes form processing a lot easier.
09:01Let's go ahead and move on.
Collapse this transcript
Traversing documents
00:00If you have done any web development in the past, then you probably know by now
00:04that traversing across the information returned from a document is something
00:09that's a very common operation and jQuery provides a lot of functions that make
00:14this process easier.
00:15Now, I have listed some of the functions that jQuery makes available to help
00:18with this process below.
00:19This is not an exhaustive list.
00:22Some of the functions in this area in jQuery are fairly advanced.
00:26So, I have chosen the ones that are most commonly used and you'll run into again and again.
00:31So, let's go over these.
00:34The first one that I want to go over is the size function and length property.
00:38They both do the same thing.
00:39When you execute a jQuery expression, and you get a result set back,
00:45this function or property, whichever one you prefer to use, will tell you the number
00:50of elements that are in the jQuery result set and you can use that information
00:55in conjunction with other jQuery functions to iterate over these items or do
01:00some other operations.
01:02So, we can also use this property or the function to test to see if the result
01:08of a jQuery expression came back with any elements at all, because sometimes you
01:14will need to know that information when you are doing certain things and we will
01:16see an example of that later on in the course.
01:19The next two functions, get and index, that returns an array of all the matched
01:25elements as DOM elements.
01:28Now remember in jQuery, the result set that come back from a jQuery selection is
01:33not an array of DOM elements.
01:35Those DOM elements have been wrapped inside jQuery objects, which have a lot of
01:41functionality built on top of them.
01:43So, these functions are pretty useful if you need to operate on the DOM elements
01:49themselves instead of using the built-in functions that jQuery gets you.
01:54So, the get function returns an array of all the elements and the result is set
01:58as DOM elements or you can use the version that takes the index parameter and
02:03that you will get you a single element at that index as a DOM element.
02:06And the last two functions, find and each, are also pretty powerful functions.
02:12The find function can be used to search inside a matched element for other
02:18elements that match the expression.
02:21You can imagine searching inside a div for images, for example.
02:25And then the last function, the each function, is also very powerful.
02:28The each function allows you to loop over the contents of every element in the
02:34result set of a jQuery selection and execute a function against that element.
02:40And again, this is a very powerful feature of jQuery.
02:42You will come across the need for this again and again.
02:46In fact, later on in the course, we will use the each function to solve
02:50a particular problem.
02:52So now that we have seen some of these functions and properties, let's jump over
02:57to the code and exercise them.
02:59All right, so here in the exercise_ files folder, I am going to open up the
03:03traversing_start file.
03:06You can view the finished version if you want or you can follow along with me in the start one.
03:12This is the document we have been using and it's got the paragraphs and the list items.
03:17So, let's begin by exercising some of the properties and functions we have learned.
03:22So, the first thing I am going to do is use the length property to inspect the
03:28result set of a jQuery selector.
03:31So, actually we are going to display it in an alert and I am going to display
03:36the string that says There are + and then I am going to execute my jQuery
03:41function and I am going to look for all the paragraphs tags and we can use the
03:47length property + and then say <p> elements.
03:55So, this should now show me in an alert, how many paragraph elements there are.
04:00So, I am going to save and go to the browser.
04:02Okay, let's bring this up in the browser and refresh and you can see that sure
04:09enough there are four <p> elements, right?
04:11One, two, three, four, that's correct.
04:13All right, so let's go back to the code, just to show you you can use the
04:17size function as well.
04:18So, I am going to save and refresh and you can see that the same thing happened.
04:22Okay, let's go back to the code.
04:25This time let's use the get functions to retrieve the DOM elements.
04:31So, I am going to write var elems =.
04:34This time I am going to look for all the list items and I am going to write get, okay.
04:40So, that's going to get me array of all the list items as DOM elements.
04:44And I am going to write alert, There are " + elems.length;
04:52and this is the built-in JavaScript array length operator there.
04:56Just to avoid confusion, there is our li tags.
05:01So, that should now produce pretty much the same result as the paragraph example
05:06only now using the DOM instead.
05:08So, let's save and now we are going to refresh and sure enough there is
05:12four list item tags.
05:15So, let's go back to the code again.
05:17This time, let's use just the individual index operator on the get function.
05:25So this time I am going to write alert and I am going to alert li.get at 0.
05:34So, this will come back with an alert showing me what kind of object is at index 0.
05:40We will save and we are going to refresh and you can see that sure enough,
05:45there is an object and it's an HTML li element and that's correct, that's guy right there.
05:51It is, in fact, a list item and this is a DOM element object right here.
05:55So, that appears to be working.
05:56So, if I wanted to work on that li tag using the DOM, I can now do that.
05:59All right, let's go back to the code. Okay.
06:02Now, let's get a little more fancy.
06:04Let's use the find function.
06:07So we are going to use the find function to find list items that have a class b
06:11inside of the ul tag.
06:14And you can accomplish this using selector expressions, but this is just for the
06:18sake of illustration.
06:19There are much more powerful ways to use the find.
06:22I am just going to use this example to show you what it does.
06:24So, I am going to select the ul tag and then I am going to type find and I am
06:29going to say find list items that have the b class and I am going to use my same
06:34CSS trick from earlier.
06:36I am going to set the border to 3 pixel, solid red, so you can see it.
06:42So, what should happen is all the list items that have b applied should have
06:45a border around them.
06:46Okay, so we are going to save and we will refresh this in the browser and
06:51sure enough, it worked.
06:52So, the find function found those list items.
06:55One more example, let's go back into the code.
06:59All right, now are going to use the each function.
07:01And this is where the things get really interesting.
07:03What I am going to do is use the each function to apply progressively increasing
07:08values to the style applied to the paragraph elements.
07:12So, I am going to make two variables.
07:14I am going to make a variable called leftmargin and that's going to start at
07:16zero and I am going to make another variable called border and that's going to start as three.
07:21So, now I am going to write my jQuery expression to find all the paragraphs tags.
07:27I am going to say .each.
07:28So, for each paragraph tag, I want to execute a function.
07:33And what this function is going to do is remember, inside this function for each
07:38one of these functions that's going to be called that this keyword is going to
07:41refer to each one of the paragraphs as it comes through.
07:44So, for each paragraph, I am going to set the CSS property for the border to be
07:55the value of the border variable + pixels and then solid red.
08:04So, the border is going to get bigger each time and then I am going to say (this).css.
08:11Only this time I am going to set the CSS for the margin-left of the paragraph
08:17to be the leftmargin.
08:20Then I am going to say border += 2, so the border is going to get thicker by 2
08:25pixels each time and the leftmargin is going to get bigger by 10 each time.
08:32So, for each paragraph, these values are going to get larger and we can see the
08:36visual result of that when we refresh the page.
08:39So, save and go back to the browser.
08:42So, now when I refresh, you are going to see each one of these paragraphs should
08:46become increasingly indented to the right and they should each have a red
08:50border that gets progressively thicker and you can see that that's exactly what happened.
08:55That's how you use jQuery to traverse across the elements in a web page.
Collapse this transcript
Understanding jQuery statement chaining
00:00We are getting to the point in the course now where we are going to start doing
00:04some much more powerful jQuery coding and before we do that, I want to point out
00:08one of jQuery's most powerful features and that is its ability to chain multiple
00:13functions together and this basically allows you to perform several operations
00:18in just one line of code.
00:20jQuery terminology calls this statements chaining and you can see an example
00:25there on the screen.
00:27So, this is a selector in jQuery just like I have been writing up until now and
00:31what I have got here on the end is a dot and then a series of functions.
00:35These functions will get executed in order, starting here on the left and going
00:38across to the right.
00:39So, you can imagine that the selector will execute.
00:41It will come back with a result set of elements in the web page and then I can
00:45do multiple operations on those guys. I can assign CSS.
00:48I can manipulate the content.
00:50I can turn events on and off, that kind of thing.
00:53And the reason why this makes jQuery so powerful is that it allows you to
00:57perform multiple operations on one result set without having to get that
01:01result set over and over and over again, like you might have to do in other
01:06scripting conventions.
01:07So, we will see this later on in the course a lot more often, but I wanted to
01:11call it to your attention so that you know what it is when you see it.
01:15And again, this is one of the things about jQuery that just makes it powerful.
01:18It's that you can assign the result set to be operated on by multiple functions.
01:22All in one line of code.
01:23And again, we will see this later on but I just wanted to call this out, so that
01:27when you see it, you know what it is. Okay.
01:29So, let's move on to building a practical example using what we have learned
01:33so far and the example that we are going to build is annotating some page
01:38links in a webpage.
01:39Let's take a look at how that works.
Collapse this transcript
Practical example 1: Annotating page links
00:01So now, we are going to take what we have learned in jQuery so far
00:04using selectors and filters and apply our knowledge to a pretty neat practical example.
00:12So, if you go into the folder for the example files, you will see a couple of
00:15files here underneath.
00:16AutoPDFIcons and there is a start and a finished and you can go ahead and jump
00:20to the finished one if you want to, or you can follow along with me here in the start version.
00:24Let me open this up in my text editor and let me show you this file in the Design view.
00:29So, you can see here that I have got a file and inside the file is a whole bunch of links.
00:34There are different kinds of links.
00:36So, if we go to the Split view, you can see what I mean.
00:39So, here is this link and it goes to an HTML file, this one here happens to be a
00:44named anchor, this one goes to another HTML, this is one goes to PDF, a couple
00:48of more HTML files, another PDF file, this one goes to a mailto link.
00:52The thing is though, if you look at the design, you can't really tell that visually.
00:56You really can't tell that Link #3 is a PDF file versus an HTML file.
01:01So, we are going to write a one line script in jQuery that automatically
01:06decorates all the links in a page with little icons that indicate that they go
01:12to PDF files rather than HTML pages.
01:15So, what I am going to do is go into the Source view and by now you should be
01:19familiar with, including the jQuery library and here is our function that gets
01:24executed when the document loads.
01:26See, what I am going to do is I am going to write a little jQuery that says get
01:32all of the links that have attributes named href and if the href attribute ends
01:38with a .pdf string, then we want to include a little icon.
01:42So, remember ends with, for attribute filters, is the dollar sign equals and we
01:48want it to end with .pdf.
01:52So, that query should get me all the links that end with .pdf and I am going to
01:58write a little function now.
01:59We haven't learned this in jQuery yet, we will see this in the next section.
02:02This has to do with manipulating content and creating some new content in the fly.
02:07So, what we need to do is insert an image that references the icon and remember
02:12that this query will come back with a set of objects, of all the links that
02:16match this criteria.
02:17So, for each one of those guys, I am going to say, hey!
02:21Insert into the document after each one of those instances.
02:24So insert an image and if you look in the example files folder, you will see
02:30that there is a folder names images.
02:31So, I am going to point at the images folder and in there you will see a little
02:36file named small_pdf_icon.gif and to make it look better, we will just align it
02:45with the absolute bottom and then we will close off the image tag.
02:52Okay, so this is going to loop through all the links and it's going to find all
02:56the links that have an href that end with PDF.
02:58So, it's going to skip over the HTML.
03:01It's going to skip over the named anchor, going to skip over the mailto and it's
03:05just going to find these guys right here.
03:08So, we are going to save that and we are going to bring it up in the browser.
03:11So, let's go out to the folder and we are going to just double-click on this
03:16folder and bring it up and you can see that when the page loads, that
03:20function gets executed and now those icons have appeared after the links that end with PDF.
03:27And we can get pretty creative with this.
03:29We can put the links before, we can put it after.
03:31We can do whatever we want, but this is a nice practical example of using jQuery
03:35to do something really simple in the page that increases the usability.
03:39And if you go back to the code, you will see that if I add more PDF links, I
03:44will just copy and paste them in and then save it, you will notice that this
03:48behavior just automatically appears on all of the links that get added.
03:51So now, anytime, anyone adds a new link to the page that ends with PDF, it will
03:55pick up this behavior.
03:56So, let's go back to the browser and let's refresh and you can see that as those
04:01links got added, so did my little icons.
04:04So, it's early on in the course, but already we are building some practical
04:08solutions to some very real problems using jQuery and we are going to keep right
04:12on doing that to the rest of the course.
Collapse this transcript
3. Manipulating Page Content
Creating, getting, and setting content
00:00Although retrieving content from the page is pretty cool in and of itself,
00:05typically that's not enough.
00:06Usually you need ways to manipulate the page content once you have it.
00:10And luckily jQuery gives us some really good ways of doing that and that's what
00:15we're going to examine in this section.
00:18Once you've used the selectors and filters to get the content out of the
00:22webpage, typically you're going to want to do something with it.
00:26In addition to that, there were times when you're going to want to create new
00:29content to add it into the page dynamically.
00:31For this purpose you use jQuery's content manipulation functions.
00:36As you're about to see, there are functions for creating and copying and
00:39deleting and moving content around as well as wrapping page content inside other content.
00:44In addition to manipulating content, jQuery provides some really good
00:48cross-browser support for working with CSS information including positioning,
00:53and sizing, and that's what we're going to take a look at.
00:56Let's look at getting, creating, and setting content.
01:01So to create new HTML content, you basically just pass a string containing the
01:06new HTML to the jQuery function, and put something like this.
01:11The result of this statement will be a new jQuery object, a new wrapped element,
01:16a result set if you will,
01:18that contains the HTML that you passed in as the string there.
01:23And since it's just a string, it also works really well if you want to pass in
01:28a variable as well.
01:29So if you can make it a variable, call it My String, pass it into the header and
01:33that will works fine.
01:35In addition to this method, you can use jQuery's html() and text() methods to
01:42get and set content on elements.
01:45And if you look at the table here you'll see what these methods do.
01:48So the html() method with no arguments basically returns the current HTML
01:54content of the first matched element that comes back from the selector's result
01:59set that you are calling this function on.
02:01However, if you pass it some content, then that sets the HTML content of every
02:06single matched element in that set.
02:09Similarly, the text() functions work pretty much the same way.
02:12You call it with no arguments and that means get the text content of the
02:16first matched element.
02:17Otherwise if you pass it some text, it will set the text content for all
02:21the matched elements.
02:23The main difference between text and HTML is that if you pass HTML code to the
02:27HTML function, it will actually create real HTML for you.
02:31If you try to pass a string into the text function, however, that contains HTML,
02:35the text function will automatically escape it for you.
02:37It will change angle brackets into text representations.
02:41It's not going to actually create HTML code.
02:45Let's jump over to the code and take a look at some of this in action.
02:54Bring it to Design View.
02:55You can see this is very similar to the file that we've been using.
02:59We have some paragraphs, we've got some list items.
03:02So we are going to go back into the Source.
03:03What we're going to do now is test out the HTML function to see what it does.
03:08So we're going to write jQuery here and let's pass in a selector that gets us
03:16that unordered list.
03:18And let's get the HTML for it and let's display that in an alert. Okay.
03:28Run this in the browser, and you can see that the alert is displaying the HTML
03:32content that is inside of that UL.
03:36So let's go back to the code.
03:37Let's get a little bit more fancy.
03:38Now, let's change the HTML of that list and see what we're going to comment this one out.
03:43Let's write something like this, pass in the list, and this time we're actually
03:49going to set the HTML.
03:52We'll set the HTML to just some new list item.
03:55This is a new list item.
03:59So now, when we run this, you're going to see all the list items that
04:03are currently there.
04:04They're going to be blown away because we're changing the HTML that's
04:07going inside the UL tag.
04:09So let's go back to the browser.
04:10So let's refresh and you can see that all those list items were replaced by the new one.
04:18Let's keep on going.
04:19Go back to the code editor. So here we are.
04:22Let's comment that one out. All right.
04:24Let's try something else.
04:25Let's try creating some new content.
04:28So I am going to write var newItem = and we're going to pass a string to the
04:34jQuery object and we'll make a new paragraph.
04:38I will say this is a new paragraph, and we will close that off.
04:42So now we've created the new paragraph.
04:46Let's insert it somewhere.
04:48So let's get a reference to paragraph 2 right here and let's say okay,
04:55that HTML is now going to be set to the new item's HTML.
05:04Let's run that in the browser and see what happens.
05:07I am going to refresh, and you can see that the contents of that paragraph
05:13have now been changed.
05:15Let's try one more thing.
05:17This time we'll use the Text function.
05:19Comment these guys.
05:21Let's try one more thing. Let's do something like this.
05:23We'll say let's get the last paragraph and we'll set the text to be, "This is
05:32the last paragraph."
05:39Let's try running that. We'll save, go over to the browser.
05:42Okay, let's refresh.
05:44So you can see how you can create and get and set content using a couple of very
05:49basic jQuery functions.
05:50So let's move onto our next lesson.
Collapse this transcript
Manipulating attributes
00:00Now, let's talk about how you can manipulate the value of individual
00:04attributes on elements.
00:07jQuery provides a couple of functions for doing this, and they are all named
00:12attr in one way or the other.
00:13And you can see in this table the various ways you can call the Attribute
00:17function attr, and there is also a function for removing attributes.
00:22So there is a couple of ways you can use the Attribute function.
00:24First, if you want to retrieve the value of an attribute, you simply call the
00:29attr function with the name of the attribute that you want to retrieve.
00:33So for example, if I want to get the value of the source attribute on an image,
00:38I would simply write attr and then place in the attribute source, src.
00:43You can also set properties on attributes.
00:46There is a couple of ways to do that.
00:48This version here, attr, that takes a properties argument.
00:51This sets a series of attributes on all of the matched elements.
00:56Remember you're calling this on the result of a selector and filter probably.
01:01So we'll set the attributes on all the matched elements using regular JavaScript
01:06Object Notation syntax.
01:08This is best used for setting large numbers of properties at once.
01:11You can see an example here.
01:13So in this case, I am passing in on an image, and I am calling the Attribute
01:17function and passing in values for Source, Title, and Alt.
01:22That's done using a JavaScript object.
01:24However, if you want to just set one single property to a value, that's pretty simple.
01:29That's this version right here.
01:30You pass in the name of the attribute and the value you want it to be and that
01:33sets the value of that attribute.
01:36A neat little feature of jQuery though is that you can pass in the name of the
01:40attribute that you want to set and you can also tell the attribute function that
01:44there is this little function right here that's going to be used to compute the
01:47value of what the attribute should be.
01:50This is really useful when, for example, you're in a loop and you're using index
01:54values to calculate values of an attribute.
01:58Say for example, suppose you're looping through a series of images and
02:02you're numbering things in ascending order, this little function right here
02:06would be used as a callback.
02:07So as each attribute is set, you can use a function to calculate what its value should be.
02:12Then finally there is the Remove attribute function and that removes an
02:17attribute from all of the matched elements in the set.
02:21Now that we've seen how to inspect and change attributes, let's jump over to the code
02:25and take a look at some examples.
02:30So a couple of things you notice right away.
02:32Here in the source code, actually let me put it into Design view really quick.
02:36You can see that on the page what I've here is an image that's in the design surface.
02:41And what we're going to be doing is manipulating the attributes on this image
02:45using the jQuery Attribute function.
02:48So let's go back to the Source View, and you can see that that's down here in
02:53the body, here is the image, and it's wrapped inside of a link tag.
02:58Let's try a couple of different things.
02:59So first, let's just preview this in the browser.
03:03You can see that the link, when you click on the image, it's going to go off
03:05to the Images folder.
03:06Let's bring this up in the browser.
03:08So when I click on this, you see that it goes to a much larger version and
03:11it's linked directly to the image itself.
03:14Suppose however we wanted to have link tags in our page open up new browser
03:18windows. We can do that really easily.
03:21So how do we do that?
03:23What we would do in that case is something like this.
03:25Write a jQuery expression that would get all the link tags in a document, and
03:31then we would set the attribute for target to be blank.
03:37Now remember, if you were to type this on a link tag in HTML, this means when
03:44the user clicks on it, it tells the browser to open the page in a new window.
03:49Here, we're just doing this automatically.
03:50This will look through all the link tags on a page and set the target attribute
03:54to be blank, so that all links will open in new pages.
03:57I am going to save that and here we are back in the browser.
04:00So we're going to refresh, and now when I click on this, you can see it's
04:04showing me the image in a new window.
04:06All right, let's try something else.
04:08Let's close this and go back to the code.
04:11So let's comment this out.
04:12So what I am going to do now is remove an attribute.
04:15So I am going to do the same thing.
04:16I am going to get a reference to all the link tags and now I am going to
04:20remove the href attribute.
04:24I am going to save that and go back to the browser.
04:27I am going to refresh.
04:28So now you can see that the little border has disappeared from the image,
04:32because what's happened is I've removed the href from the link, which means that
04:36as far as the browser is concerned, this is not a valid link anymore.
04:40So if you want to turn links on and off, this is a way to do it. One last example.
04:46Let's go back to the code.
04:47So let's try setting attributes as a properties object.
04:51So to do that, what I am going to do is write jQuery statement that gets the
04:56image, and I am going to write attr.
05:00Only this time, I am going to use an object to set the attributes.
05:04So I am going to set the source attribute to be a different image and it's
05:08going to be images/Spring.jpg.
05:15I am also going to set the Alt tag to be the word Spring.
05:21So I am going to save, switch over to the browser, and I am going to refresh.
05:26You can see that when I loaded it, it changed the source of the image to the
05:30Spring image, and it also changed the Alt tag, although we don't really have
05:34the way of seeing that.
05:35But that's how you can set multiple properties on a single object. Okay.
05:40That wraps it up for attributes. Let's move on.
Collapse this transcript
Inserting content
00:00Let's take a look at inserting content using jQuery.
00:04So, jQuery provides several functions for inserting content into the document
00:10and for modifying the document object model's DOM tree.
00:16And you can insert content into the document before and after existing page elements.
00:21You can also insert content inside of elements.
00:24There is a bunch of functions to do this.
00:27So, let's go over them.
00:28Append and Prepend functions allow you to insert content inside of other elements.
00:36So the Append function takes the content here and that will append the content
00:42inside every one of matched elements that this function is being called on.
00:47Conversely, the Prepend function will take content and put it at the beginning
00:52of the inside of every matched element.
00:55The appendTo and prependTo work pretty much the same way.
00:59Although they're called on jQuery selectors.
01:04So, in this case, you're specifying the content to append; in this case you're saying
01:08here is the set of matched elements I want to append or prepend content to.
01:12So, that's for inserting inside of elements.
01:14The after and before are kind of analogous to the Append and the Prepend functions.
01:20So, the After function takes content and inserts it after each one of
01:25the matched elements.
01:27The Before function inserts content before each one of the matched functions.
01:32And we saw an example of the After function in one of our previous lessons when
01:36we were building our annotated page links.
01:39The insertAfter and insertBefore again, they work the same way.
01:42Only in this case, they take jQuery selectors to insert matched elements before
01:47or after another set of matched elements that will match this selector.
01:51Let's take a look at some examples and we'll see how this works.
01:56Here in the exercise_files folder, I am going to open up the inserting_start file
02:01and this is the finished version right here.
02:04So, let's go ahead and do that.
02:05I am going to open this up in my editor.
02:06So, you can see we're back using pretty much the same document that we've been
02:11using for a couple of different examples now.
02:13This is the one with the unordered list and some paragraphs.
02:16So, here's what we're going to do.
02:18Let's jump back over to source code.
02:20Let's try out some of these functions.
02:21So, first thing I am going to do is write a jQuery statement for paragraphs.
02:27So, I am going to get all the paragraphs in the document and I am going to
02:31append some content to them.
02:33And what I am going to append is a string that says "with some content appended."
02:42Okay, so if we scroll down you see that This is paragraph 1, This is paragraph 2
02:46and we're going to add the string on to the end of each one.
02:49So, let's go ahead and back out to the browser.
02:50We're going to start this up.
02:53And you can see what happened was each one of the paragraphs now has the string
02:56with some content appended on it.
02:58That content was appended to each one of the matched elements and in this case,
03:02let's go back to the code.
03:03We've got all of the paragraphs.
03:06If I were to do something like a filter like last or first or something then
03:10only those paragraphs would have been affected.
03:12So, let's try something else.
03:15Let's do the opposite.
03:15Let's do p, only this time we're going to prepend.
03:19I am going to write, "Hello!" Okay.
03:25So now the same thing is going to happen this time.
03:27I am going to save and we're going to back out to the browser.
03:30Okay, now when we refresh, you can see that in this case the word Hello has
03:34been prepended or inserted at the beginning of each paragraph. Back to the code.
03:38Let's get a little more fancy.
03:40What we're going to do now we're going to use the appendTo and prependTo
03:44functions for manipulating the DOM tree.
03:47So what we're going to do is write a jQuery accessor that says get me the last
03:53paragraph and append that to the first paragraph.
04:01Let's save that.
04:02Go to the browser and see what happens.
04:04I am going to refresh.
04:07And you can see what happened is paragraph 4, which used to be down here at the bottom,
04:10has now been appended to paragraph 1.
04:14So, we took the last paragraph and appended it to the first.
04:18Let's do a variation on that theme.
04:20Comment this one out.
04:22But using that as the basis for the next one.
04:24What we're going to do in this case is instead of appendTo, we're going to say prependTo.
04:31Okay, so now we're going to save.
04:34Go back to the browser.
04:35So, now I am going to refresh it and you can see now that the last paragraph has
04:40been prepended in front of the first paragraph.
04:43Okay, so I think you pretty much get the idea.
04:45This pretty much wraps up using the content inserting functions.
04:49That should give you an idea of how you can insert content into your document
04:52and use these functions to move elements around within the DOM tree.
Collapse this transcript
Wrapping, replacing, and removing content
00:00In this lesson, we are going to see how to use jQuery for wrapping, replacing,
00:05and removing content.
00:07Up until now, we have seen how to insert and manipulate some content, but now we
00:11are going to do a little bit of additional operations.
00:15So, in addition to inserting content, jQuery has a bunch of functions for doing
00:20some additional operations like wrapping content in the page, replacing it,
00:24copying it, removing it, all that great stuff.
00:26So here is the list of functions that you use in order to do that.
00:30And as you can see there is a half-a- dozen functions just for wrapping the content.
00:35So, let's take a look at those.
00:37The Wrap functions basically takes matched elements and wraps them
00:41inside something else.
00:43So, in this case for example the Wrap function will take each one of the
00:48elements on the matched set that you are calling the Wrap function on, and place
00:52them inside the HTML that you specify here.
00:54So, for example if I was calling the Wrap function on a whole bunch of images,
00:58and I passed the String, like DIV inside here, then each one of the images will
01:02be wrapped inside it's own separate DIV.
01:05Similarly, the Wrap Element works pretty much the same way.
01:09Each one of the elements inside the matched set that this is being called on,
01:12will be wrapped inside the element that I specify here.
01:16The wrapAll works pretty similarly although what it does is rather than wrapping
01:20each one of the individually matched elements into it's own wrapper.
01:25It will take all of the elements inside the matched set and place them in either
01:30the HTML that I am specifying here, or the element.
01:33And finally, the wrapInner functions will wrap the inside trial contents of each
01:40matched element, including text within either HTML or the DOM element that
01:46specify here assuming that the DOM element that I am passing is allowed to have
01:51children inside of it.
01:52So, for example Images can't have child elements inside them.
01:55So that would work.
01:56If I call wrapInner on an ul tag and I had a whole bunch of allies, then all
02:01those allies will be wrapped inside whatever I passed in as this HTML or this
02:05HTML or this element.
02:07So that's wrapping you can also replace.
02:10So the replaceWith function, you give it some content, and it will replace
02:15all of the matched elements in the result set with whatever content you specify here.
02:21And content either be HTML or a series of DOM elements.
02:26And the replaceAll basically replaces the elements that are matched by the
02:30selector with the matched elements.
02:32So, this selector here is going to be used to find which should be replaced by
02:37the all the elements that are being called on.
02:40And the last couple of functions are some pretty convenient functions, Empty
02:44basically removes all the child notes from all the elements that are in the set,
02:48and you are calling it on.
02:50Remove just removes all the matched elements from the DOM.
02:53So, what you do is you write your jQuery selector, then you Call, Remove and
02:56then all those elements taken out the document.
02:59Then the Clone function makes copies.
03:01So, this Clone function clones the matched DOM elements and then selects all the clones.
03:07This version here which takes a Boolean argument, if you pass True then it will
03:13clone not only the elements, but it will also copy all the event handlers,
03:18whereas this one will not.
03:20Okay so that's a trip down that function set.
03:22Let's take a look at some examples in the code.
03:25I am going to open up my wrapping_start file right here.
03:29The same document we have been working on.
03:31The list is here paragraphs, and so on.
03:33Let's do a couple of experiments.
03:35So let's wrap all the paragraph tabs, and let's wrap each one into a DIV who's
03:45style has been set to color:red.
03:50So all the text inside those DIVs should be red.
03:54We need to put that in a tag.
03:56So, let's do that and then let's close the DIV off.
04:00Okay, so let's try that out, go to the browser and see what happens.
04:04So, you can see that the paragraphs two and four.
04:07The ones that did not have classes set on them, have now changed to Red text.
04:13And that's because these guys do not have any style set, so their text color is
04:18being affected by the DIV that it was wrapped into.
04:21Let's try another example.
04:22Let's go back to the code.
04:24Let's try wrapping all of the paragraphs in a DIV whose style is let's just see, border:
04:333px solid red, save.
04:37And go back to the browser and let's refresh.
04:41You can see that the wrapAll functions has taken all of the paragraphs, and
04:46wrapped them inside a DIV whose border is now red, and three pixels wide.
04:51Okay so far so good.
04:52Let's go back to the code, let's comment this guys out.
04:55All right, now let's try the remove the Empty function.
04:59So, I am going to write a expression here that gets the unordered list and I am
05:04going to call Empty, and go back to browser and refresh, and you can see that
05:08the list items have now all then emptied out from the unordered list.
05:14Okay, so that should give you a pretty good idea of how these functions work.
05:17Now you have seen how to create content, you've seen how to insert it, and now
05:22you're going to go have to do some other manipulations on it like wrapping and
05:25replacing and removing.
Collapse this transcript
Working with CSS
00:00jQuery's CSS functions provide easy cross-browser access for setting properties
00:06as well as working with positioning and sizing information for elements that are in your webpage.
00:12The CSS function allows you to retrieve and set styles for a set of matched
00:17elements, and up until now throughout this title you've probably been seeing me
00:21use that CSS function to make some of the early examples in this course visible
00:26to you to on the screen.
00:27So we are going to take a few moments and learn what it actually does.
00:31So it has a few different forms.
00:33The first and simplest form is this one right here, CSS, and you give it a named property.
00:38This will return the value of the named, right here, CSS property for first
00:44matched element that you specify here in the result set that you are calling this on.
00:49The next version right here is CSS with the properties argument.
00:52This will set the CSS properties of every matched element in the result set
00:56using in object notation syntax, and this is very similar the attribute example
01:01that we saw earlier.
01:03You can see here is an example of just creating an object that has a couple of
01:06properties and values to go with each one and then calling the CSS function with
01:11that object to set the properties on the results of a jQuery expression.
01:17But if all you want to do is set a single style property to a value on all of
01:21the matched elements, you can do that right here.
01:23This simply takes the name of a property like width or height or opacity or
01:27whatever, and sets it to whatever value you give it.
01:30Now if you just provide a number here rather than string, it's going to be
01:33converted automatically into a pixel value, except for certain CSS properties,
01:38and you can see those listed here like z-index, or font-weight, or opacity, or
01:43zoom, or the line-height.
01:45Those do not take pixel values.
01:47So that won't happen there.
01:48jQuery also provides a set of functions for working with CSS classes.
01:53Again, this is used very commonly in things like animation or interactivity.
01:57You are setting styles on elements and taking them off and jQuery provides
02:01functions for adding and removing, toggling, and detecting these classes.
02:06You can see those in this table.
02:07So as you might expect the addClass will add a class or classes to each one of
02:14the matched elements.
02:15The hasClass function will return true, if the specified class is present on
02:22at least one of the elements that are in the result set that you are calling this on.
02:26The removeClass is the opposite of addClass.
02:28It just simply takes that class off from all the matched elements.
02:31Then there is the toggle function.
02:33So the toggleClass will add this class if it's not there already and it will
02:36remove it, if it is present.
02:38You can also call it with switch.
02:39You can say hey, add it if the switch is true or remove it if the switch is false.
02:45So moving on, let's take a look at the positioning functions.
02:48So CSS positioning functions provide a cross-browser way of figuring out where
02:53elements are on the page.
02:55There is a variety of functions for this.
02:57So the offset function gets the current offset.
03:00This comes back within object that has a top and a left.
03:03So same thing here.
03:05This is the offsetParent.
03:07It figures out the position parent of the first matched element in the result set.
03:12Position also comes back with a point, a top and the left.
03:14This gets the top and left position of an element relative to the parent
03:19that it's inside of.
03:20Then the rest of the functions deal with scrolling.
03:22So for example this scrollTop will either get or set the scrollTop offset of
03:28either the first matched element in the case of getting it or you can set
03:32the scrollTop offset for all the matched elements in a result set, and
03:35the same thing for the left.
03:38Then finally the CSS sizing information.
03:41You can figure out how big elements are by using a variety of functions that
03:44jQuery gives you, and again these work across browsers.
03:48So the height functions will either get the current computed at by the browser
03:53pixel height of the first matched element in the set or you can set the height
03:57of all the matched elements.
03:59Same thing for width.
04:00You get the current width as it's been computed by the browser or you can set the width.
04:04The innerHeight and innerWidth will, for example, get the inner height and
04:08that means excluding the border, but including the padding for the first matched element.
04:15The same thing for innerWidth.
04:17This will figure out how wide something is, but again excluding the border and
04:21including the padding.
04:22Then outerHeight and outerWidth work just the opposite.
04:25They will get the outer height, which includes the border and padding by
04:30default for the first matched element.
04:32If you pass true for the margin, then the margin values are also included.
04:37The same thing for outerWidth.
04:38It will get the outer width as it's been computed by the browser and again you
04:43can ask to have the margin included.
04:44So that's a lot to digest.
04:46Let's take a look at some of these functions in action.
04:50So in the exercise folder, I have a file right here called css_sizing and I am
04:55just going to bring this up in the browser, so you can see what's happening.
04:58So you can see that we have a div here on the page and I am using some jQuery to
05:03compute various pieces of information about this object.
05:07So you can see I have got the Height and Width here and the innerHeight and
05:11innerWidth, and the outerHeight and outerWidth and the offset and the
05:16position in the page.
05:18So what I want to do now is go back to file system and bring this up in Firefox.
05:21What you are going to see is that when Firefox comes up, and let me unmagnify
05:26this and move it over.
05:27You can see the values are pretty much the same for both browsers.
05:32They are both 180, they are both 250.
05:34You can see down here as well that the offset here is a little bit different for
05:39the offset and position.
05:40So let's go into the code and see that.
05:44The position is being calculated by the position and the offset is being
05:50calculated by the offset.
05:51Now the offsets, remember, are a little bit different for each one of those guys.
05:56The top and left relative to the document.
05:58So here what we were doing is here are all of the spans.
06:01We are calling each one of these functions.
06:03So for the height, I am calling the height function, the width function, and
06:06you can see here when the results are filled out that that's where the
06:12values all go and you can see that that's how the browser is measuring each one of these guys.
Collapse this transcript
Associating data with page elements
00:00jQuery provides a couple of very useful methods for associating arbitrary data
00:05with elements on the page.
00:07There are several scenarios where this is pretty useful.
00:10For example, you might have an image on a page, and you might want to associate
00:14some data with that image such as who the photographer is, and when and where
00:19the photo was taken. Or maybe you have a series of div elements on a page, and
00:24each one of those divs contain some information about, say, an employee record,
00:29and you want to associate some data at runtime such as the employee name and
00:33their position and so on.
00:34jQuery makes that really easy, by providing a couple of methods that are used
00:39for working with data.
00:40So, here on the jQuery site, I'm going to jump over to the API documentation, and
00:44that takes me to api.jquery.com. And I am going to search for data, or
00:49alternatively, I can also just scroll down here to the Data category. Either one
00:54of those works. And here is the method we are going to look at.
00:57We're going to look at the data method.
00:59Now the data method is used to store and retrieve data associated with elements
01:03on the page, and there's two ways of calling it.
01:06This group right here with the key and the value, and this instance here, the
01:10object, that's how you store data on a page element, and then the second group
01:14down here is how you retrieve the data.
01:17Now, the way data is stored is by keys and values.
01:21So for example, in the first way of calling it here, you give it a string,
01:25which is the key that you want to associate the data with, and then the value
01:29is any JavaScript object.
01:31It can be a number, it can be a string, it can be an array, it can be a complex
01:36JavaScript object. Or you can just give it an Object which contains a whole
01:40bunch of key-value pairs to put the data into the page element.
01:46Then the other method we want to look at is the removeData method.
01:50So, let's go back down here. removeData is how you remove data from an element
01:57or set of elements on the page.
01:59Again, you can give it the element to remove the data from, and if you want, you
02:04can give it an optional string, which is the individual key for the data you
02:09want removed. Or if you just don't provide any key name at all, then all of the
02:13data will be removed.
02:14So, let's jump over to the Example Files folder.
02:17I have got my data_start.html page right here, and this is the file we're going
02:25to use to exercise these methods.
02:26So, I am just going to double-click on it and bring it up in the browser, and
02:28you can see I've a DIV here on the page with some buttons
02:32that are going to associate and otherwise exercise our data method.
02:38Let's go over to the code and write the code for this example.
02:42Here we were on the code for that file. This is my data_start file.
02:47Here is my reference to jQuery library.
02:49Here are the buttons down here that are going to trigger each one of these methods.
02:53Now, we haven't yet covered how to associate events with elements in jQuery, and
02:59we will see how to do that later.
03:01So, don't pay too much attention right now to these click functions. How these
03:04work will become clear later on in the course.
03:08All you need to know is that's how I am going to attach event handlers to each
03:11one of these buttons to exercise the methods I am going to be showing. All right!
03:15So, for the first one what we're going to do is when the user clicks on the
03:20store button, we want to store data with the div.
03:23So, here in the event handler for the store button, let's just go ahead and
03:28write some code to do that.
03:29We're going to get the element, which is I believe div1, so I'll give it the id for div1.
03:37That's this id right here.
03:40So, will get #div1, and we will say .data, and we will give it a key, and I'll
03:47just use the name key1. Then we have to give it the data we want associated with
03:52the div and I'll just give it a number 1234.
03:54We will store another piece of data on a different key, so we'll do the same thing.
04:00We will say data. And in this case I'll call it key2, and this time I'll give it a string.
04:09I'll just use my name.
04:10Okay, so that's how we store data on the div element.
04:15What's going to happen is the button will be clicked and then for these two
04:19keys, we will just store some arbitrary data, and then our program can use this
04:22data however it sees fit.
04:24To remove the data when the user clicks on the remove button, we're going to
04:28get a reference to our div, and we'll just simply call removeData.
04:36That will remove any of the data that is on the div.
04:41Finally, we have to have some way of making sure this is all working.
04:44I'm going to have a function here in the show event handler
04:50that's going to just display a couple of alerts to see if there's any data.
04:54So, we will just say alert.
04:56We will get the div here, so that's div1.
05:02This time we're going to call the data function, and we're going to pass in the
05:07name of the key that we're interested in, but we're not going to pass in any
05:11other parameters, because now we want to retrieve the data.
05:13We don't want to set the data.
05:15We'll get the value for key1, and then we will just copy this line and do the
05:19same thing for key2, and that should be good. All right!
05:23Let's go back to the browser and refresh the page and see if it works.
05:27I am going to refresh the page here.
05:29First I'm going to click show data without having set anything, right?
05:33And you can see that when I do that I get an alert that has undefined in it, and
05:38another one says undefined. That's because I haven't stored the data yet.
05:41Let's go ahead and click store data.
05:43Now the data has been stored on the div.
05:45Now let's click the Show button one more time.
05:47We can see that's the first piece of data.
05:49That's the 1234 that I set.
05:51That was the number I supplied, and then the next one is my name. OK.
05:56So now let's click the Remove button, so now the data has been removed from the
06:01div and when we click this, once again we're back to undefined. OK.
06:06Let's do one more thing.
06:08Let's show how you can work with data attributes.
06:11This is a new feature in HTML5.
06:15Data attributes basically allow you to put data directly on elements inside a page.
06:21The way you do that is right here on the div, I'm going to come down here to the
06:24div and I am going to give it an attribute. I am going to name it data-key3.
06:30I'm going to give it a value of, I don't know, I'm just going to say data attribute.
06:38These data attributes, which begin with data-, and this is a part of the
06:43HTML5 specification.
06:44This allows me to associate data directly within the element, and then these
06:48data attributes will automatically show up whenever I try to use the data method in jQuery.
06:54It will read the contents of any data attribute I have on my elements and make
06:59them available to me via the data method.
07:01Let's try that out.
07:02Let's go back up the show function, and let's comment these two lines, put in
07:07another line, and this time what we're going to do is I'm going to say alert.
07:11We're going to say let's get the ("#div1").
07:13We're going to get the data() method.
07:20And now we're going to call it with no arguments whatsoever, because now we want to
07:23get all of the data that's associated with the element.
07:27So, we'll close that off and we'll save and we will go back to the browser.
07:33All right, back here in the browser, I am going to refresh.
07:37Now, I haven't stored any data yet, but watch what happens. When I click show
07:41data, you see okay, there is an object that's on the element, and it's showing up
07:45as an object in the JavaScript alert.
07:47So, let's go back to the code and make it show up as a string.
07:50Okay, so back here in the code, I'm going to take that object and I am going to
07:57show it as a string.
07:59And the way that you do that is you can use the browser's built-in JSON object.
08:03So, I am just going to type JSON and I am going to write stringify, okay.
08:09We will give it the data element, and it takes a couple of other parameters.
08:15So the first one is going to be what's called a replacer element, and I am not
08:19going to use it, I am just going to pass in null, and then I am going to
08:21pass in a couple of spaces, which will cause the JavaScript object to be
08:27converted into a string and pretty printed with these spaces.
08:31All right, I'll save. Back to the browser, refresh, click on show data, and you
08:38can see now that the key3 attribute, which is already in the source code, has a
08:44string value of data attribute.
08:46And notice that the data- part of the key name is stripped off. That's not part
08:50of the actual attribute name; that's just what the browser uses to indicate that
08:54something is a data attribute.
08:56So everything following the data- is what the actual attribute of the data is
09:02when it's put into the data handling method of jQuery.
09:05So, let's click OK.
09:07Now, I'm going to store the data, like I did the first time around.
09:11You'll see that when I click show data, the key that's already on the
09:16element via the data attribute is there, along with the two keys that I added
09:21via the JavaScript code. All right!
09:23So, that's how you can associate arbitrary data with page elements using the
09:27jQuery data methods.
Collapse this transcript
Practical example 2: Automatic TOC generator
00:01All right, in this example, what we are going to do now is take what we've
00:03learned in this chapter and we are going to build a practical finished example.
00:08The example what I am going to build is an automatic table of contents for a document.
00:13If you look inside the exercise files, you will see these two files right here,
00:19AutoTOC_start and AutoTOC_finished, so I am going to build this example starting
00:24from the starting point and you could either follow along with me or you could
00:27just go ahead and jump right to the finished version.
00:29Before I do that though, I am going to bring up the finished version in the
00:33browser so you can see what it's doing.
00:35So let's just double-click on this guy.
00:37You can see that this is a document that's got a whole bunch of you know Lorem
00:41ipsum text in it and there is a whole bunch of these headings, right.
00:44There is the Introduction.
00:45There is this heading here and so on.
00:47This right here, this Table of Contents, contains a bunch of links that jump to
00:52the various places in the document.
00:55So for example, if I click on this guy, you see it jumps right down to that
00:58heading and I go back. I can jump to this guy down here and it goes right to that heading.
01:02This Table of Contents, what's happening is this is being built up automatically
01:06by some jQuery code.
01:08So we are going to see how to build that automatic TOC Generator, which will look
01:15through the document for various kinds of headers and then use those to build
01:21named anchors inside the document where we want to jump to.
01:25Okay, so let's do that.
01:27I will close this and I am going to bring this up in my editor.
01:33So here is the starting point and what we are going to do is write this function
01:38here called buildBookmarks.
01:40You can see that when the document.ready event fires that function is being called
01:45and it's being called with two arguments.
01:48The first argument is the type of header tag that we want to serve as the TOC
01:53locations and the second argument is the id of the div that we want to append
01:59the resulting Table of Contents HTML into.
02:03So you can see that that's this div right here that has the header id on it.
02:08So that's where the title is.
02:10This is where the subtitle is.
02:11So we are going to build that Table of Contents and put it in right at the
02:15end of this div here.
02:17The other thing you should realize also is that each one of these sections in
02:20the document is enclosed in a div and has an id and there is these h3 tags that
02:26are at the top of each one of the sections.
02:29These are the headers for each section in the document.
02:32So what we are going to do is write some jQuery code that builds this bookmark list.
02:38So what we are going to do right off the bat is write a jQuery statement that
02:43creates the unordered list that's going to serve as the table of contents.
02:49So, the way we are going to do that is just tell jQuery to make me a new list.
02:56So we are just going to say make me an unordered list whose id is equal to bookmarksList
03:07and we'll just close that off. Okay.
03:10So now I've got my new unordered list there.
03:12So for each one of the header tags, there is two things we need to do.
03:17First, we need to create a named anchor and insert it into the header tag
03:22because that's going to serve as the jump location for the links in the TOC.
03:27Then we need to add that link into this unordered list right here that will
03:32point to the named anchor.
03:35So what we are going to do is get all the divs, because the sections are
03:39all divided into divs.
03:41So I am going to write a jQuery that says get me all of the divs but I don't want,
03:46so we'll just say not the div where the id attribute is equal to header,
03:53because we are not going to be looking inside this guy right here.
03:56That's where the TOC is going to go.
03:58So get me every div that doesn't have an id that's equal to the header, okay,
04:04plus, so we'll look inside divs, but we want the h3 tags.
04:07That's what we need.
04:09So I am going to add on the strWhichTag argument and you can see strWhichTag is
04:16the h3 that's being passed in.
04:18So we are looking for h3s inside the divs that are not the header div.
04:23Now I am going to get a little bit tricky here.
04:27So recall from earlier in the course when I covered the Traversing section there
04:31is a function called each, and I am going to use the each function and the each
04:37function is going to iterate over each one of the contents that we find, because
04:42there is two things that we have to do.
04:44So I am going to write a function and my each function is going to do a couple
04:51of different things.
04:52The first thing it's going to do is create the named anchor and since we need
04:57named anchors to be unique in the document, we have to have unique way of naming them.
05:02So what I am going to do is create a little variable up here named AnchorCount
05:08and I am going to initialize that to 0, and we are going to increment that
05:11value each times so that.
05:12That way all the named anchors have unique names.
05:15So what I am going to say is inside the each function, I am going to write this,
05:20because the this keyword will refer to the h3 tag.
05:24So I am going to set the html for the h3 tag to be a named anchor so, a name =
05:34and then the name is going to be bookmark and we are going to add the
05:42AnchorCount to it, so that's going to be cAnchorCount. Okay.
05:46So now we are making the unique named anchor and we are going to close that off now.
05:51So we close off that named anchor tag and that's going to set the HTML to the
05:57named anchor, but now we need to add in the existing HTML for the h3.
06:00We don't want the h3 to be blown away.
06:02So we are going to say add this.existing html.
06:09So what that does is take the existing HTML for the h3 and just put a named
06:14anchor right in front of it.
06:17So now we have created the named anchor, what we need to do now is add our link
06:21to the list that we are creating.
06:23So to do that, we are going to write oList.append, so that's the list.
06:30So we are going to say oList.append and we are going to make a new list item.
06:34So we are going to use jQuery to do that.
06:36And I am just going to pass the jQuery object some html.
06:40That's going to be a list item, and inside the list item, there is going to
06:43be an anchor or a link and the href is going to go to the named anchor for
06:50the bookmark, right.
06:52That's this guy that we made up here, plus the cAnchorCount and then we are
06:57going to increment the AnchorCount, Plus.
07:01So closing HTML, plus the text that's inside the h3.
07:09And remember to get the text that's inside the h3, we'll write this.text.
07:15So we have created the anchor. We need to close off the named anchor tag.
07:19So before I forget that, we need to close off the list, the link, and we need to
07:26close off the list item.
07:28So now we have created the named anchor, we have created the list item and
07:32we should be pretty much good to go at this point.
07:36So let's scroll back on over here. Okay.
07:40So now after we have finished creating the content that's going to go in the
07:44list and the named anchor when this loop here terminates, we need to put the
07:50list of links we just created inside this div.
07:55So now we are going to say jQuery, get me the id for the BookMarkNode, right.
08:05Remember it's passed in as a variable and that's going to be header.
08:09So get me the id for the div, .append, and we are going to put the list into that div.
08:20Okay, one more time.
08:21Let's just see what this is doing.
08:23So we have a variable that keeps track of a counter, so that all of our
08:27named anchors are unique.
08:29We create an unordered list.
08:31That's going to be the list of TOC.
08:33For every div that's not the header, plus the h3s, we are going to loop over
08:38each one of those guys and we are going to create a named anchor that goes in
08:42front of the h3 tag.
08:43That's the link destination.
08:45Then we create a list item that has the link into it that jumps to that named
08:49anchor and so we do that for each one of the h3s and when we have done that,
08:54we put the entire list into the div.
08:56All right, here we are on the browser and you can see that when I bring up the
09:01document, that's exactly what happened.
09:03So now each one of these h3s has a little invisible named anchor in front of it.
09:07So when I click, it jumps right there and you can see that the text of each one
09:15of these link items is the same text as the h3.
09:20Okay, that pretty much wraps it up.
09:21Now you know how to use content manipulation and retrieval to solve a
09:25real practical problem.
Collapse this transcript
4. Working with Events
Understanding the jQuery event handling features
00:00In this chapter, we're going to handle working with events in jQuery.
00:05So jQuery provides a mechanism for working with events that is simpler than
00:10relying on the Document Object Model.
00:13And if you've ever worked with events in modern webpage development, one of
00:17the things you've probably noticed is that it's pretty frustrating because the
00:20different browsers have different ways of working with the modern event implementations.
00:26Specifically, IE has its own way of attaching events to and disconnecting them
00:32from elements, which is separate from the DOM Level 2 Specification, which is
00:38what a lot of the other browsers follow.
00:41And because jQuery works with sets of elements by default, it's very easy to
00:47write code that assigns event handlers to groups of objects, just by using the
00:52results of the selectors and filters that we've already seen.
00:57So jQuery events basically break down into a couple of categories.
01:01The first is the Binding and Unbinding category.
01:05These functions allow you to wire events up, and take them off of elements in a
01:10simple cross-browser way.
01:12And we'll see an example of that in a moment.
01:15The next is the Unified Event Object.
01:18Another thing that you've probably noticed if you've done web development or
01:22cross-browsers with modern events is that the event object is slightly different
01:27between the IE browsers and other browsers, and jQuery solves that problem by
01:32creating a unified event object that exposes the properties and methods that you
01:38use most commonly in a cross-browser way.
01:43Then finally there is a bunch of convenience features and these are basically
01:47functions that encapsulate a lot of common event features and helper routines
01:52that work cross-browsers and we'll see an example of those as well.
01:55So now that we understand what event handling in jQuery is and what we are going
02:00to do, let's see a good example.
Collapse this transcript
Binding and unbinding events
00:00Let's start by seeing how to use jQuery to wire up event handlers to elements
00:06on our page. And so here in the API Documentation section on the jQuery site,
00:11I'm just going to scroll on down to the Events section, and I'll click on Event
00:16Handler Attachment.
00:17And the functions that we're going to be covering are the on and off functions.
00:22On is used to attach an event handler for one or more events to the selected
00:28elements in the page, and then the off function removes an event handler.
00:32Now, you might see older jQuery code using functions like bind and functions
00:37like unbind, and there are functions like live and die.
00:41These functions are from older versions of jQuery, and you can see that some of
00:45them have already been deprecated.
00:47So, for example die and live, they've been deprecated as of jQuery 1.7.
00:51Bind and Unbind still work, but at some point, they'll be deprecated too, and the
00:56on and off functions are pretty much the way forward for attaching events.
01:01So, let's take a quick look at the on function.
01:03Here, in the on function, you can see it takes a variety of arguments.
01:07The Events argument is a space-separated string for the events that you want to
01:13attach, for example click or mouse down or mouse up or whatever.
01:19The selector argument here is optional, and that's used to filter out
01:24descendents of the selected elements that are going to trigger the event.
01:28And we're not going to cover that right now, but we will see it a little bit later.
01:32The data argument can be anything.
01:33That's just any arbitrary data that you want passed to the event handler when
01:38the event is triggered.
01:39So, if there's some data that you want to use inside your event handler, you can
01:43do that. And then there's the handler argument, which essentially is the function
01:46that's going to be triggered when the event happens.
01:49That's where your event handling code will be.
01:51Enough of me just talking about it; let's go over to the code and actually see this in action.
01:56So, what I'm going to do is bring up the binding_start file in the browser.
02:00So let me just launch that.
02:02And this is the test file that we're going to be using to exercise these functions.
02:05You can see that right now it doesn't do anything.
02:09But when we get finished, this will be causing some change in the page when I
02:13mouse over and mouse out of this element. Let's go over to the code.
02:16Here in the code for my example file, a couple of things I want to point out.
02:22First, we have a couple of style sheets, one called normal, one called
02:25highlighted, and those will become obvious in a moment.
02:28Down here I have a div.
02:29It has an ID of evtTarget, and the current class is the normal class, which is
02:36this one right here.
02:37And we're going to wire up some event handlers for when the user mouses over and
02:42mouses out of this div, and we're going to respond to that event by changing the
02:47class to the highlighted class.
02:49So, this will be changing between yellow and red as the mouse comes in and
02:53out of the element.
02:54I'll start off by getting a reference to the event target element.
02:58So, I'm just going to write #evtTarget.
03:02That gets me my target element. And I'm going to write on.
03:07Inside on, I'm going to pass mouseover mouseleave.
03:14These are the two events that I want to attach a handler for, and I'm going
03:19to write highlight.
03:22Highlight is a function that I'm going to write that handles the event.
03:26It's going to be my event handler function.
03:29Let's go ahead and write that right now.
03:30I'm going to put that actually outside of my document ready function.
03:34So I will write function highlight, and that takes an event argument because
03:41it's an event handler.
03:42Inside the highlight function, I'm going to get a reference to my evtTarget again.
03:48And this time I'm going to use the CSS helper function called toggleClass, and
03:57toggleClass is basically going to either remove or add the highlighted CSS style
04:05sheet, which is this guy right here.
04:07So, let's go ahead and save that.
04:08Let's just go ahead and run what we have right now.
04:10So, I'm going to go back to the browser, and I'm going to refresh.
04:15Now, you can see that when the mouse is over the element, the highlighted CSS
04:20class gets added, and then when I mouse out, it gets removed.
04:24So that's the toggleClass in action right there.
04:27Let's go back to the code for a second.
04:29Now, let's add a handler for the mouse- clicking event which will turn off the
04:37highlighting behavior.
04:38I'm also going to add a handler here for my evtTarget.
04:44And in this case, I'm going to say on("click". This time I'm just going to
04:52pass an anonymous function right in here which is an event handler, so it takes an event.
04:58And inside this event handler I am going to do two things.
05:00First, I'm going to get my evtTarget, and I'm going to do the off function.
05:09So now, I'm going to turn off the mouseover and mouseleave event handler, which
05:18is the highlight function.
05:22After I turn off the event, I'll just put in some content in the div that says hey!
05:28You know, you turned it off.
05:29So I'll say ("#evtTarget").html, and in there I'll just put a little paragraph
05:38tag that says "You shut off the hover effect!"
05:46Now, when the user clicks on this, I'm going to turn off my mouseover mouseleave
05:52events and put some HTML content in there.
05:56And we'll close it off with a semicolon, and we'll save.
05:58Okay, let's go back to the browser.
06:02Now, let's refresh. Okay.
06:03So you can see that the hover effect is still there.
06:08And now when I click, you can see that now the content says, "You shut off the hover effect!"
06:13And now the hover effect is no longer working.
06:17So, that's a basic introduction to connecting events to elements using jQuery.
Collapse this transcript
Convenient event helper methods
00:00In addition to basic functions for wiring up events in jQuery, there are also a
00:05whole bunch of higher-level event handling functions that you can use to handle
00:10common situations with events.
00:11For example, if I'm here in the API documentation, I'm going to scroll on down
00:16to the Events section and click on Mouse Events.
00:18And you can see that there are event handlers for things like click, and
00:22double-click, hover, mousedown, and mouseup, and so on.
00:25These are all a whole bunch of common high-level events that will happen in
00:31your web pages, and jQuery provides some nice high- level event handling functions to deal with them.
00:37And so we're going to exercise some of those in this particular lesson.
00:41Let's go over to the Example Files folder.
00:44Here in the Example Files I've got my helpers_start document, and I'm going to
00:50bring that up in the browser.
00:53Again, this is pretty much the same example we used in the previous movie.
00:56We're going to use the helper functions this time in order to hook up the hover
01:01event, along with a couple of other things.
01:03Let's go over to the code. All right!
01:06So here in the code I've got pretty much the same file as I had before.
01:10This one has the div.
01:12It's got the style sheets normal and highlighted, only this time we're going to
01:16write the code using the helper functions.
01:18So, recall in the previous example I used the on function to bind events for
01:23mouseover and mouseleave.
01:25However, jQuery gives me a function that already does that.
01:28So, I'm just going to write dollar sign, and I'm going to get my evtTarget div.
01:34And I'm going to use the hover function.
01:36And the hover function basically takes two functions, and those two functions
01:42are what you want to happen when the mouse enters something and when the mouse leaves.
01:46In this case, they're both going to be the same function, so I'm going to pass
01:51highlight and highlight.
01:53Now, I need to write the highlight function, so I'm going to write
01:57function highlight.
01:59And it's an event handler, so it's going to take an event argument, even though
02:03I'm not going to use it.
02:04Inside the highlight function I'm going to write the code to get the event
02:10target div, and I'm just going to toggle its class like I did last time, and the
02:15toggleClass is going to be highlighted.
02:20Okay, that is this class right down here, which will basically change the
02:25background color to red. All right!
02:27So, I'm going to save, and let's go back to the browser.
02:33Let's refresh the file.
02:35And now you can see that I'm using the hover function to achieve pretty much the
02:40same result as I was achieving using the on function to bind events.
02:45Let's go back to the code.
02:47Let's use a couple other functions.
02:49Let's use a function for, let's see.
02:52We're going to get the evtTarget, and this time we'll use the click function.
03:00For the click function what we'll do is we'll call a function called fnClick1.
03:07And that's what will happen when the user clicks on it.
03:09And let's add another one. Let's do ("#evtTarget").dblclick.
03:18This is for handling double-clicks.
03:20And we'll just use the fnClick2 function for this.
03:25Now I'm going to write both of those.
03:27So, here in the function for fnClick1-- and again that's an event handler, but
03:34I'm not going to use the argument.
03:36All I'm going to do is simply set the content of the div, by using the HTML
03:44function. I'm going to have it say click for the fnClick2 function.
03:51I'm just going to copy and paste this.
03:53I'm going to write the word Double Click!
03:58So, what's happening here is, when the user clicks on the div, this function will
04:03get called, and when they double-click on it, this function will get called. So, let's save.
04:08Let's go back to the browser. All right! Let's refresh.
04:10There is the hover behavior, and now I'm going to click, and you can see that
04:16the click handler causes the content that say Click.
04:18Now I'm going to double-click, and you can see that the content changes to Double Click!
04:23I can just do that to my heart's content now.
04:26So, that's an example of using the higher-level functions to achieve features
04:31and effects that you would normally have to use by using the lower-level event
04:35binding functions, and there's a whole bunch of these.
04:38I encourage you to check them out.
Collapse this transcript
Using the jQuery event object
00:00As I mentioned earlier one of the frustrating things about writing
00:04event handling code is that there is a lot of differences among the IE browsers
00:09and the browsers that use the DOM Event Model.
00:13One of the big differences is in the way the event objects are implemented in both browsers.
00:19The IE Event Object and the DOM Event Object have a couple of properties that
00:24intersect, but for the most part they are pretty different.
00:26jQuery helps solve this problem by smoothing these differences out and
00:31providing a single event object with the most important properties that you will use commonly.
00:38You can see that here in the table.
00:40Now, I haven't listed all of the properties and methods because some of them are
00:44kind of advanced and esoteric.
00:46What I've done is listed the most common ones.
00:48So just taking a look at the properties available.
00:51The type property is essentially the type of the event that the event object is representing.
00:57So it'll be a string representing the event name, click for example, or
01:01mouse down or whatever.
01:02The target property is the element that issued the event.
01:06So looking at the event object, you can figure out which element in the page
01:09the event came from.
01:11The data property is optional.
01:13If you passed any data parameter to the bind function, this property here
01:17will contain that value.
01:19The pageX and pageY properties are the coordinates of the mouse when the event happened.
01:26That's document relative.
01:29The result property is the value that was returned by the last event handler
01:36function, as long as the return was not undefined.
01:39Then the return value will be contained in here.
01:42The timestamp is in milliseconds and that's the time when the event occurred.
01:48So, those are the most common properties.
01:50There is a couple of functions that you might want to use as well.
01:53The preventDefault() function prevents the browser from executing whatever the
01:57default action for the event is.
01:59So this essentially stops the event from causing whatever the browser would
02:04normally do with it.
02:05So for example following a link.
02:07The isDefaultPrevented() function will return whether the preventDefault()
02:12function was ever called on this event object.
02:15So to see if that was ever prevented you can check this function.
02:18And stopPropogation() stops the bubbling of an event up to the parent elements.
02:25Now, it's beyond the scope of this title to explain event bubbling.
02:28But if you are familiar with the DOM level 2, what happens is when an event
02:32happens in a child element, and this isn't true of all events, but it's true
02:37mostly for mouse events.
02:38When an event happens, if the element that it happens on does not handle the event,
02:43it will then bubble up, like a bubble in liquid, to the elements above it.
02:48You can stop that from happening by calling stopPropogation().
02:50It will simply stop the element right in its tracks.
02:53And then similarly, for preventDefault().
02:55If you call isPropogationStopped(), you can see if stopPropogation() was ever
03:00called on this particular event object.
03:04So let's jump over to the code and take a look at using the event object in an example.
03:14So here is the file.
03:17You can see we've got three divs.
03:21And what I'm going to do is in source code, I'm going to write some code
03:26that's going to respond to an event and set some information inside each one of the divs.
03:31So, here I've got my jQuery already installed, so I am kind of writing the script.
03:42When the page loads, what I'm going to do is setup some event handlers on
03:50each one of the divs.
03:51So I'm going to write a quick query to get all the divs and assign event handlers to them.
03:57So for the click function, I'm going to write a little anonymous function that
04:03takes the event object.
04:07And what we are going to do is just inside the div that was clicked,
04:13we're going to set the HTML content to be the contents of some interesting parts of the event object.
04:22So, for example well just say, let's see, pageX was and that will be evt.pageX +
04:35pageY, and that will be evt.pageY +, and we'll get the type of the event.
04:52The type will be evt.type +, and that will be the target, + evt.target.
05:13And I think that probably pretty much about covers what we're going to put on there.
05:19Okay. Let's try that.
05:20We'll save.
05:24Now, let's jump on over to the browser.
05:29So now I'm going to bring this up in the browser.
05:32You can see that as I'm clicking on each one of the divs, the event is
05:38happening, and you can see that the mouse position is being updated each time. Okay.
05:46Let's see if this works cross browser the way it's supposed to.
05:51Okay, so now I'm going to bring this up in Firefox. Okay.
05:56All right, there is the Firefox browser.
06:00Yup, you can see that it's working just exactly the same way.
06:04So we were able to really easily write some cross browser handling code that
06:10uses the unified event object to get information about events that happen.
06:17So that's the event object.
06:18Let's move on to our next example.
Collapse this transcript
Using miscellaneous event features
00:00The last series of event functions we are going to cover for jQuery are some
00:05miscellaneous event functions.
00:06And these are basically provided by jQuery to handle a couple of specialized tasks.
00:12Those functions are the one() function, and trigger(), and triggerHandler().
00:17The one() function pretty much works the same as bind().
00:21However, the event handler is only ever going to be executed once for each one
00:26of the matched elements that the event is bound to.
00:28And you call this function when you only want an event handler to happen once.
00:33So, the event will be bound, and then once the event gets triggered, it will not
00:39be responded to again.
00:41The other two functions, trigger() and triggerHandler(), are ways of essentially
00:45triggering events from within code without having to wait for the user.
00:50The difference here is that the trigger () function will trigger an event on each
00:56one of the matched elements that come back from this selector.
00:59And it will also cause the browser to act as though the user caused the event to happen.
01:06So, for example if you trigger the click event on a link, then not only will the
01:11event fire off from within your code, but the browser will act as though the
01:14item were clicked on.
01:15Now, triggerHandler() is similar.
01:18It does the same thing.
01:19It triggers the event handlers but it does it without having the browser go off,
01:23and act as though the event actually happened.
01:27This only works on the first matched element in the result set for a selector.
01:35So, what we're going to do now is take a look at some examples of how we might use this.
01:39We're not going to use the trigger() or triggerHandler().
01:41Those are pretty esoteric.
01:43So, let's take a look at how the one() event handler works.
01:46Let's jump over to the code.
01:50So, here's the source code.
01:51Just take a look at the Design really quick.
01:53So what I have here is a series of divs, and each one has a color and a border.
02:00What we're going to do is write some code that binds event handlers to these divs
02:05that only fire once.
02:06So, let's go back to the Source.
02:08So, you can see that for each one of these divs, I've defined a style sheet.
02:12And among other things it defines things like width and height and you can see
02:16that there is a cursor property here in the style sheet that's set to a pointer
02:20indicating it could be clicked on.
02:22So, what we're going to do is create an event handler that only fires the
02:28click() function once.
02:30So, let's go up here where I've got my jQuery library included.
02:34So, I'm going to write my onload handler.
02:41So, I will write function, and when the document loads,
02:51what we're going to do is get a reference to all these divs, and instead of
02:55using the bind() function we're going to use one().
02:59What we're going to do is bind an event for click.
03:03And we'll have a function that gets called.
03:10So, inside this function we're going to set on the this operator.
03:15Remember, inside-- since the click event is happening on a div, when this
03:20handler function gets called, the this parameter will refer to the div that got clicked on.
03:25So, we're going to set the css for the div that got clicked on.
03:31The css we're going to set will be, we'll change the background to red and change the cursor to auto.
03:43so it's going to get rid of the pointer.
03:44And that should be all we need to do.
03:48So, now we're going to switch over to the browser and see if this works.
03:55So, I'm just going to double- click on this, bring up the browser.
03:58So you can see that as I'm mousing over this the little pointing cursor.
04:02So I click on it once, it changes to red.
04:04You can see that the pointer went away, and if I click on it nothing happens anymore.
04:07So this is basically how you assign event handlers that only happen one time.
04:14So, let's just make sure that this works cross browser.
04:16I'm going to open this up with Firefox.
04:20Okay, and exactly the same behavior.
04:27You can see that now the event handler is only happening one time.
04:29So now that we've seen how to work with events, let's try and build a practical
04:35example and that's the subject of the next lesson.
Collapse this transcript
Practical example 3: Table striping and highlighting
00:00Let's take what we have learned so far about jQuery event handling and some
00:04other features and build a practical example.
00:07What we are going to do is use jQuery to automatically stripe and
00:13hover-highlight the contents of an HTML table.
00:16So, let me show you what the finished example looks like first.
00:20I am going to bring this up in the browser. And please excuse the garish colors;
00:24I had to make sure that they were visible on whatever screen you might be using.
00:29But you can see that the contents of this table are striped, in alternating
00:34lighter and darker colors of green.
00:36And when I hover over rows and the table, the text is bolded and is highlighted
00:44in sort of a yellowish, goldish color.
00:47So this is what we're going to build.
00:49So let me close this.
00:51And let's go take a look at the code that's needed to build this example.
00:55So let's switch over to the code. All right! Here I have the code for my example.
01:02And you can see it's just a simple HTML table with some contents in it.
01:08And before I actually build the code, let's take a look at what the barebones
01:13example looks like in the browser.
01:15And you can see that it's just a table.
01:17There's no special highlighting already applied, and the hover effect is clearly not there.
01:23So let's just build from here.
01:25So I'm going to close this.
01:27Okay, let's go back to the code.
01:30Okay, so the first thing I'm going to do is add a couple of CSS style sheets
01:34that are going to serve as the stripe colors.
01:38So, I'm going to define a class called stripe1, and I'm going to define
01:44stripe1 as having a background-color of, let's see, it's going to be very
01:51visible, so I'll just say #0f0.
01:53And then I'll copy this and make stripe2, and stripe2 is going to be background
02:01color of afa, and we'll save that.
02:07And then I need to make a CSS class that's going to serve as the hover highlight.
02:13So I'll name that .highlight.
02:18And the highlight color, what we're going to do is we're going to have a
02:21background color of, let's see, # ffcc00. Yeah, that's a good, nice yellow color.
02:30And then we're going to add in a font-weight of bold.
02:36And that's going to make the text bold. Okay.
02:38So now we have the CSS styles, so let's go back up to the top of document and
02:43fill out our jQuery function and logic to make this happen.
02:48So, the first thing we're going to do is handle the striping.
02:51So, we're going to use the jQuery selector to get all of the tables.
02:55And you can see here, down in the code, that the table itself has an ID of the list.
03:03And so what I'm going to do is use jQuery to get the list table.
03:10But I don't want the whole table; all I want is the table rows.
03:13So I'm going to use tr, and then I'm going to use one of my filter selectors
03:18here called even, so that's going to get me all the even-numbered table rows.
03:22And I'm going to add a class of stripe1.
03:28So, that's going to put stripe1 on all the even-numbered rows, and then I am
03:35going to copy this and paste it, and I am going to do the same thing for the odd rows.
03:40And for the odd rows, I'm going to add stripe2.
03:43So, let's just see if this works on what we have so far.
03:46So I am going to save this. Let's go back out to the example files.
03:50Okay, so far so good.
03:52We can see that the stripe is being applied.
03:54Just to make sure it's working cross-browser, let's go see it in something else.
03:58Let's see it in, say, Firefox.
04:03Okay, so Firefox comes out.
04:04You can see it's working there too. All right! So far so good.
04:07We've got it working cross-browser. Let's go back to the code. All right!
04:11So now that we have the table striping working, now let's add that hover
04:14highlighting effect.
04:16And so what we're going to do is once again, we're going get a reference to the
04:20table, which is theList.
04:23Now, we're going to apply it only to all the table rows, not just the even and odd ones.
04:28But we're going to use the hover function.
04:30And remember that the hover function takes two arguments.
04:34So, the first is going to be the function that we want to have happen when the
04:39mouse enters, and then we have another function, which is going to be what
04:45happens when the mouse leaves.
04:46So, when the mouse enters, we're going to say $(this), which at this point is
04:52going to be the table row in question, so $(this).toggleClass.
04:57And the class we're going to be toggling is the highlight class, and we're going
05:03to be doing the same thing down here in the second function.
05:06So now we've got our hover highlighting effect as well. All right!
05:11So let's go back out to the browser and see if this works.
05:13I'm going to save, back to the browser and refresh.
05:16And sure enough, now you can see that the highlighting effect is working,
05:21because when I hover over a table row, it changes the background color and the font-weight.
05:26All right! So far so good!
05:28Okay, now let's take a look at one more feature of jQuery events.
05:33This is called event delegation.
05:35And here's how it works.
05:37Suppose I want to have the user, whenever they click on one of the table rows,
05:43just show a little alert message that has the text contents of the table row.
05:48Now, there's a couple of ways I could do that.
05:51One way to do that would be to write theList, and then tr again, then say on, on click.
06:03Let's have a function, which is an event handler, so it takes an event handler argument.
06:10And inside this we're going to say alert and $(this).text, and that will get the text content.
06:22That will work; however, there's a problem here.
06:25And the problem is that this right here, this selector, is going to go through
06:30and assign click handlers to every single one of the table rows.
06:34Now, for a table that only has 10, 12 rows, that's okay.
06:38But imagine I had a table with lots and lots of rows, or imagine this wasn't a table;
06:43it was a whole bunch of divs.
06:45That's going to create a whole bunch of event handlers on a whole bunch of
06:48objects that basically just do the same thing.
06:51What I'd like to be able to do is take advantage of something called event
06:56bubbling in the browser, and just tell jQuery that whenever an event comes
07:01from a certain kind of element but makes its way back up to the element's
07:07parent, have the parent handle the event, but only if it came from a certain
07:12kind of descendent.
07:13So, what I'm going to say here is get me the table, and I'm going to attach the
07:18click handler to the table itself.
07:19But I'm going to use the delegate trick that we talked about earlier to say
07:24only handle events when they come from table rows.
07:29So, if a click event gets triggered on the table and it came from a table row,
07:36then execute this function.
07:39So now I've only got one event handler, and it's sitting on the top-level
07:43parent table, and it's listening for click events that came from table rows.
07:48So rather than having a table with a thousand rows in it and having a thousand
07:53separate click handlers, I've only got one click handler, and it's listening for
07:57clicks that come from within table rows.
07:59And browsers handle this automatically.
08:01When something like a click event happens on an element and the element doesn't
08:05handle that event, such as a click,
08:07that click will then bubble up the DOM until it gets to a parent element which
08:12does have an event handler.
08:13And so by attaching the event handler for click directly to the table itself, I
08:17only have one place where I need to listen for it. All right!
08:21So let's save, and let's go back out to the browser. Let's refresh.
08:26All right! So, there is the striping, there is a hovering, and now when I click, you see I
08:32get an alert that has the text content of the row.
08:36In this case, it's bread, and it's $0.99, and I can do it here.
08:39And you can see I'm getting different values for each one of the rows that I'm clicking on.
08:45So, only one event handler, and it's listing for events from that particular
08:50table row, and that is what event delegation is all about.
08:54That brings us to the close of jQuery events;
08:57time to move on to our next chapter.
Collapse this transcript
5. jQuery Animations and Effects
Hiding and showing elements
00:00One of the hallmarks of modern web applications, if you have used the web at all
00:05recently, you have probably noticed that there is a lot of great animation and
00:09effects on these modern web pages.
00:12And it used to be that you had to use plug-ins to achieve a lot of
00:16these animation effects.
00:18Well, jQuery makes providing animation and effects easy to uses of your site.
00:25And in fact in many cases you can do some pretty neat things with just one
00:29or two lines of code.
00:31And that's going to be the subject of this chapter.
00:34So the jQuery library as I noted supplies some basic animation and effects
00:39functions to create some visual effects that are pretty common in modern web
00:44pages, such as showing and hiding elements, fading elements in and out, moving
00:51them around on the screen.
00:52There is also a basic animation function that you can use to build your own
00:58animation effects, and we will see how to do that later on in the chapter.
01:04But first, let's just get started with some really simple animation and that
01:09involves showing and hiding elements.
01:11So showing and hiding elements are probably one of the most simple effects that
01:16you can use in jQuery.
01:18And you can exercise some pretty nice control over how elements are shown or hidden.
01:24They can happen immediately or over a specified duration of time.
01:28And you can see in the table there is a series of functions you can use.
01:32So not surprisingly, the show() functions show elements, the hide()
01:37functions hide elements.
01:39And then there is a set of toggle() functions, and these basically do what
01:43you'd imagine, they show something if it's hidden and they hide something if it's shown.
01:47And there is a couple of different ways of doing each one of these.
01:50The most basic show() function simply displays each one of the elements that
01:56come back from a selector if they are hidden, and that just shows them.
02:00The next version though allows you to specify the speed with which things
02:05happen, and you can either pass a string like slow, normal, or fast or you can
02:11pass a number, which is a millisecond duration.
02:13So 2000 milliseconds would be 2 seconds.
02:17And there is an optional callback that you can have jQuery call when the
02:22animation has become complete.
02:24So for example, when an element is finished showing, if you want to do some
02:28of the UI changes, you can supply a callback function that will be notified
02:32when the effect is done.
02:34The same thing for hide.
02:36There is the speed and callback function.
02:39And same thing here with toggle, the speed and callback.
02:42And this one here, the switch basically pass true or false and it either hides
02:47or shows everything based upon whether they are hidden or shown.
02:51So now that we have seen what some of the functions are, let's jump over to the
02:55code and try some of these out.
02:58So here I am in the browser and you can see I have got a nice big blue div and I
03:03have got some buttons down here that trigger some functions.
03:06So it's currently visible, so if I click Show, nothing happens.
03:09But if I click Hide, you can see that the element goes away.
03:12If I click Show, it brings it back.
03:15And if I click Toggle, Toggle just does the opposite of whatever it currently is.
03:20That's pretty basic.
03:21Now let's look at the code to see how that's happening.
03:24So I am going to bring this up in my editor.
03:31Before we look at the code, just take a look at the Design view.
03:33So here is the Design view, you can see here is the element, that's the div and
03:37here are the various buttons, so let's go to the Source.
03:41So down here in the Source, this is div right here.
03:45And here are each one of the buttons, and the buttons have various id's on
03:50them and up here you can see that the div has a style sheet applied to it that
03:57has some appearance.
03:59And up here in the script, and you can see that when the browser loads the page,
04:04I am assigning some click handlers to each one of the buttons.
04:09So the Show button gets the show() function, the Hide button gets the hide()
04:13function, and so on.
04:15So what I am doing here is passing various parameters to the function.
04:19So for show, I am passing in a speed of normal but I can also pass in a speed of
04:23something like slow, right.
04:25And if I go back to the browser, I will refresh.
04:30I am going to hide it and now when I click Show, you can see it comes in
04:34a little bit slower.
04:35But I can actually slow that down a lot more.
04:36Let's go back to the code.
04:39So instead of passing slow, what I will do is I will pass in a duration of
04:435 seconds, and I will pass in a callback that tells me that the animation is complete.
04:51And for the callback, I will just say alert("done showing").
04:58So let's go back, save.
05:00Let's go back to the browser, let's refresh.
05:03Now I am going to Hide it, and now when I click Show, you see it comes in very
05:08slowly over 5 seconds.
05:10And there is my alert that says hey, all done showing.
05:14That's pretty much it for hiding and showing elements.
05:17Let's look now at how we would fade elements in and out, another very
05:21common effect.
Collapse this transcript
Fading elements in and out
00:00Fading elements in and out is also a common effect that you see in modern web pages,
00:06and jQuery provides functions for achieving this effect as well.
00:10You can fade images or other elements either completely in or out, or you can
00:16fade them to a predetermined level of opacity, so that they are transparent.
00:21And you can select the speed of how fast things are fading either in or out by
00:27using strings like slow, normal, or fast, or as a millisecond duration.
00:32And you can see here in the table there is three routines that perform fading.
00:36There is the fadeIn.
00:37There is the fadeOut and fadeTo.
00:40FadeIn does what its name implies.
00:42It fades in all the elements that are in the matched set, that a selector came
00:48back with, which is what you're calling this function on, and you can specify
00:52the speed that you want this to happen at.
00:54And there is an optional callback that will run after the effect is finished.
00:59So if you've got something that you want to happen when things are faded either
01:03in or out, you can supply a callback function, and that function will get called
01:08once the effect is done.
01:09Similarly, there is a fadeOut function.
01:12It does the opposite of fadeIn, and here is fadeTo.
01:15And for fadeTo, there is a speed and callback function, but there is also the
01:19opacity level that you want the object to fadeTo.
01:22So, anywhere between zero and 1.0, that will be the percentage of transparency
01:28that the object will fadeTo.
01:31Let's jump over to the code and see how these work.
01:37So, here we have the div element, and nice big blue div, sitting in my
01:40document, and I've got a couple of buttons down here, and the buttons will
01:44perform various functions.
01:47So, I've got Fade in and Fade Out and then a couple of values for Fade To.
01:51So it's already faded in, let's try fading it out.
01:53So when you fade it out, you can see it slowly disappears, fading it in makes it
01:57slowly appear and we can also fade it down to 30% and we can fade it up to 100%.
02:05So, lets take a look at the code and see how this works.
02:09I am going to bring this up in my editor, and you bring it up in whatever
02:14editor you are using.
02:16You can see that there are some jQuery code there that's using the fade
02:19functions, but before we take a look at that, let's scroll down here.
02:21You can see here is the element and here are the buttons and I've got my various
02:27buttons for fading elements to various states.
02:32So let's scroll back up to the code.
02:34So, here we are when the document is ready, we assign some event handlers and
02:39each button gets a click handler.
02:41So, for fading in, we have a fadeIn function and fadeOut, and again, I can
02:45control how fast I want these to happen.
02:48So I've got some speeds that I specified here, but if I want something to fade
02:53out very slowly, instead of using normal, I can specify duration like 4000 and
02:59that will fade out after 4 seconds, and I can supply a function that will get
03:05called when its done, and let's just write a little alert that says done
03:11fading out and save.
03:14Let's go back out to the browser, let's refresh.
03:20Again, the fading works.
03:23When I click Fade Out it's going to very slowly fade over 4 seconds and my
03:29callback, it gets called.
03:31So, let's just quickly make sure that this works cross-browser like we say it
03:35does, let's bring this up in Firefox.
03:37I am going to open this up in the Firefox, and there it is, fading to 30%,
03:45fading to 100% and we can fade it in, and let's fade it out and it goes over 4
03:53seconds, here is my alert, let's fade it back in.
03:59That's quick summary of the fading functions.
Collapse this transcript
Sliding elements
00:00The sliding effect is yet another way that jQuery gives you to reveal or
00:06hide page elements.
00:08These functions are not for sliding things around on the screen, just doing
00:13some generic movement. This is for showing and hiding elements but using a sliding effect.
00:19jQuery provides functions for sliding elements up or down, as well as toggling
00:25the slide animation based upon whether things are hidden or showing.
00:30And you've probably seen this effect before in web pages, where you click an
00:34Option button and things kind of slide into views seemingly from thin air, and
00:39that's how this effect is achieved in jQuery.
00:43The slideDown function allows me to specify the speed at which the slide
00:47animation happens and there is an optional callback function that will be called
00:52when the slide is completed, and the same thing for slideUp.
00:55I can specify the speed and speed can either be a string like slow or normal
01:00or fast or I can specify a number of milliseconds that I want the animation to happen over.
01:07And SlideToggle is the same idea.
01:09It just toggles whether things are sliding up or sliding down based upon whether
01:13they are already showing.
01:15Let's take a look at the sliding examples.
01:19I am going to jump over to the code.
01:23So here is the file in the browser and I've got the big blue div right here.
01:28And I've have got some buttons that trigger some behavior.
01:30So it's already visible, so lets slide it up, and see that it slides out of
01:35view, and sliding down makes it visible, and when I try to hit Slide Down again,
01:41nothing happens because it's already visible and similarly Slide Up there is
01:44nothing once it's hidden.
01:46The Toggle behavior however is, well, it just simply takes it from whatever
01:51state it's currently in and brings it to the opposite state.
01:54So let's take a look at the code, and see how that happens.
01:56I am going to open this up in my editor, so let's scroll down really quick
02:04before we see the code.
02:06Here are the buttons that are controlling the behavior.
02:08You can see these IDs on them, and this is the div right here and the div has a
02:13style applied to it, which sets its initial appearance, these are all the
02:17styles I have on my div.
02:19So let's scroll back up.
02:21So, when the page loads and the document is ready, I set up some event handlers.
02:26So, for the slideUp button, I assign a click() function that calls the slideUp
02:30function on the div and for the slideDown button I call the slideDown function.
02:35And for toggle the same idea, I call the slideToggle.
02:38So, let's have a little bit of fun with this.
02:41We can take out the normal here, and I can put in something kind of slow like 4
02:47seconds, so 4000 milliseconds is 4 seconds.
02:51And I can also define a function that will be called when the slide up is complete.
02:57So I am just going to display an alert that says slide complete, and again, you
03:06would do this if you had some UI that you wanted to manipulate or otherwise
03:10control when the effect was done with.
03:14So, now I am going to save and jump to the browser again.
03:17Now, I am going to refresh.
03:20You can see that the Toggle Slide still works pretty smoothly, but when I Slide
03:26Up, it's going to slide up very slowly over 4 seconds and then show my alert.
03:32So, let's make sure that works in Firefox as well.
03:38Open this in Firefox, just to prove it's cross-browser.
03:42So, here is the same file.
03:44You can see toggling works, and I am going slide up very slowly over 4 seconds
03:50and there is the alert.
03:54So, that's a quick summary of the jQuery sliding functions and using those
03:58functions, you can easily slide elements into and out of view.
Collapse this transcript
Creating custom animations
00:00In addition to the pre-built animation functions that jQuery gives you, there is
00:06a basic Animate function that you can use to create custom animation for a wide
00:11variety of properties on page elements.
00:14So to do that, you simply call the Animate function and there are a couple of
00:17different versions you can see there on the table.
00:20One takes a series of parameters and those are the properties on the element
00:24that you want to animate.
00:25There is a Duration parameter and that's the number of milliseconds that the
00:30animation should take.
00:31The Easing parameter is the type of easing function to use and there are only two built-in.
00:36There are plug-ins that do more, but right now jQuery has Linear or Swing built-in.
00:41Finally, there is Call Back option and the Call Back is the function that will
00:47be called when the animation is completed and that of course is optional.
00:52There is also another more complex animate function here and it's same idea,
00:57these are the properties you want to animate and the options are a set of
01:02options set in an object notation style for the animation that you want to have happen.
01:08There is also a Stop function and the Stop function basically stops all the
01:13running animations on all of the specified elements.
01:16So you would have some jQuery selector that will produce a result set, and then
01:21you would call Stop on all those guys to stop them from animating if they are.
01:26So let's take a look at how to use the animate function.
01:30I am going to jump over to the code and we can see how it works.
01:33You will see that there is a div on the page with some text in it.
01:39So let's try some custom animations.
01:42The first animation is when we click the Grow Right, you will see that the side
01:46of the div grows out to the right.
01:48If we click the Big Text, you will see that the text animates up in size.
01:53If we click Move Div, you will see that it animates over to the right.
01:57So let's refresh, and let's see if it works in Firefox as well.
02:03So I am going to bring this up in Firefox. There it is.
02:08So I am going to click Move, I am going to click Big Text, and I am going to
02:12click Grow Right, and you can see that it worked.
02:14Now, let's refresh.
02:15Now let's go over to the code and see how we accomplish this.
02:19I am going to bring this up in my Editor.
02:27Down here are the buttons that control the animation.
02:32Here is the Style Sheet on the div, and that's the div that we are
02:35animating right there.
02:37Up here, you can see that what's happening is we are calling various versions of
02:42the Animate function with parameters that we want to have animated.
02:47So the way this works is I get a reference to the element or elements that I want to animate.
02:53And in this case, it's just one, but again, I can have a result set that comes back.
02:58I call the Animate function with an object.
03:01Inside the object, I list the properties that I want to have animated.
03:06So sin this example for the width, I am setting a Width property on the object
03:13for the parameters I am passing in, and I want that to happen over the course of one second.
03:18So in this case you can see I have got the different Animate functions
03:21controlling different properties.
03:22So this is the one for the width, this is the one that animates the font
03:25size, this is the one that animates the left side property, which is what
03:29makes it move over.
03:30However, since we are specifying things as objects, I can have multiple of these
03:36animations appear all at once.
03:38So let's copy this function here, and let's make a new button called Multiple.
03:47So that's going to be its ID.
03:48I am going to go down here and I am just going to copy this guy, and name it
03:55Multiple, and we will label the button Everything.
04:02So when the Everything button gets clicked, let's animate everything.
04:06So we will animate the width.
04:08We will copy that and we will paste that in here.
04:12Let's animate the font size and we'll copy that and we will paste that in here.
04:21You notice also that I am passing in a parameter for the Easing function.
04:25I am passing in Swing, which is kind of like a Smoothing function that makes the
04:29animation appear bit more natural.
04:31But I could also pass in Linear if I wanted to for this parameter, and we will
04:35see what effect that has.
04:37So now, instead of animating just one thing at a time, I have got a parameters
04:41object that will animate all of those properties over the course of one second.
04:46So let's save and let's go back out to the browser.
04:49So now, I am going to refresh.
04:54So now when I refresh, you see that the Everything button is there.
04:56So now when I click the Everything button, you can see that everything
04:58happens all at once.
05:00The font size is animated up, the div got bigger, and it moved over to the
05:05right-hand side, because I animated the left.
05:07Let's switch over to Firefox.
05:08So I am going to refresh here as well.
05:11There is my Everything button.
05:14When I click on Everything, you can see that everything happens all at the same time.
05:19You can see that using the Animate function, you can create some pretty
05:22complex animations just by specifying the parameters that you want to animate
05:26from and animate to.
05:29So the default, let's go back to the code really quick.
05:33When I specify these parameters like Width, 500, this is going to be the ending result.
05:37This is what the property will look like when the animation is complete.
05:40The starting value is whatever the Style Sheet sets it to.
05:44So you can see here that the Width starts out at 250, and it's going to end up
05:48at 500, and similarly, for the rest of the properties as well.
05:51So that's how you use the Animate function.
05:53We are at the point now where we can build a practical example.
Collapse this transcript
Practical example 4: Image rotator
00:00 What we are going to do now is take what we have learned about events and
00:05 the previous chapters and we are going to build a pretty sophisticated effect.
00:09 And what we are going to do is build an image rotator that has a nice
00:13 fading effect in it.
00:15 So before I show you the code, let me bring up the finished image rotator in the browser.
00:20 So if you look in the Exercise Files folder under the Effects chapter,
00:24 you will a couple of files.
00:25 There is the imagerotator_ start and imagerotator_finished.
00:28 So let me just bring this up in the browser.
00:31 You can see that what's happening is over a couple of seconds, we have a nice
00:35 little image slideshow where the images are fading from one to the next and
00:40 they are just doing this in a circular pattern.
00:43 So there are four images and each one is fading on top of the next.
00:49 Let me bring this up in Firefox too, so you can see that it works there.
00:54 Let's bring up in Firefox browser. Same idea.
00:59 So you can see that it's slowly fading across the images.
01:02 What I have done is built a cross- browser image rotator and there are no
01:06 plug-ins involved here.
01:07 This is all just JavaScript and jQuery.
01:10 So let's see how that works.
01:11 What I am going to do is bring up the Start version of the file in my Editor,
01:18 and let's go to the Design View really quick.
01:21 You can see here in the Design View, I have got an image.
01:23 There is actually more than one image.
01:25 I have positioned them so that they all stacked on top of each other.
01:29 So let's go to the Source View and you can see that.
01:33 So here is the images that we are going to rotate.
01:37 So a couple of things.
01:38 First, everything is contained inside a parent div and the parent div I have
01:42 got here is named photoShow and then I have got four divs inside that.
01:48 Each one of those divs contains an image.
01:52 The first div has a class applied to it named Current.
01:56 So you can see that inside each one of these divs
01:58 there is an image pointing to a path and each one has a height and a width.
02:05 What I have done here is define these four images and let's take a look at the
02:09 styles applied to each.
02:11 The photoShow div itself has a height and a width applied and that matches the
02:17 height and width of the images that I am rotating.
02:20 Then the divs inside the parent div are positioned absolutely.
02:25 So that's causing all the images to stack up underneath each other.
02:29 By default, I am giving each image a z-index of 0.
02:32 So they're going to be underneath at the bottom of the stack.
02:34 Then I have got two other style sheets, one called Previous and one called Current.
02:39 The Current style sheet basically has a z-index of 2.
02:43 So whichever div has that Style Sheet applied is going to be the one that is on
02:48 top of all the other images, and you can see that that's this div, and that
02:51 image is inside the div.
02:53 So this one will be on the top of everybody else.
02:56 Then we have another Style Sheet named Previous and that has a z-index of 1.
03:01 So whichever div has this Style Sheet will be directly under this guy right here.
03:07 Now, that's not initially assigned.
03:09 We are going to assign that dynamically as part of the rotation effect.
03:14 So now that we have got all the contents set up, let's take a look at the code.
03:18 So I have got my jQuery Library included, and what I need to do now is write the
03:24 code that sets up the image rotator.
03:28 So what I am going to do is write a function that executes when the page gets loaded.
03:34 This is the jQuery document ready event and what I am going to do is call
03:40 setInterval on a function I am going to write called rotateImages and I am
03:47 going to have that run every two seconds, so 2000 milliseconds.
03:51 Okay, so now, I need to write the function to rotate the images.
03:54 There are a couple of things we need to do.
04:00 What we are going to do is figure out which photo is the current one and which
04:04 photo is the next one.
04:06 What we are going to do is take whatever the current photo on top is.
04:10 We are going to move it underneath the photo that comes next.
04:15 So that's going to be the image that fades up.
04:17 So we are going to move the current photo underneath the next one, set the
04:21 next photo's Opacity to 0, and then have it fade up in front of the photo
04:28 that's in front of it.
04:30 So I am going to have a variable here named oCurPhoto and I am going to use
04:37 jQuery to get a reference to whichever photo is current.
04:42 So what I am going to do is look for inside the photoShow div,
04:48 I am going to look for whatever div has the current Style Sheet applied to it,
04:53 because that's the current photo.
04:54 Then I am going to look for the next photo and again I am going to use jQuery for that.
05:01 So to get the next photo, recall from our selector and retrieving content
05:05 section that there is a function called Next.
05:08 So I am going to say oCurPhoto.next.
05:12 This will get the next sibling after wherever this current div is here.
05:17 So if this div is current, then the next div is this guy and if this div is
05:22 current, then this is the next guy.
05:25 If this div is current, there is no next.
05:27 You can see that we are at the end of the div list.
05:29 We have to handle that condition.
05:30 So if that's the case, then the jQuery result that comes back from the next
05:35 function will be of length 0.
05:38 We can check that condition.
05:39 So we say if oNxtPhoto.length,
05:44 in other words, there is nothing in the jQuery result set, is equal to 0,
05:48 then what we need to do is go back to the top of the loop.
05:52 And to do that, remember using jQuery, we are going to say well oNxtPhoto = and
05:59 what we are going to do is simply get the first div that's inside the photoShow
06:09 parent div, which will be this guy right here.
06:12 So that's how we are going to get the looping behavior.
06:14 So now we've figured out which div is current and which one is next, we need to
06:18 do the CSS animation that causes the Opacity to animate.
06:22 So what we are going to do is tell the current photo to remove the class that
06:31 is current, because it's no longer the current photo, and we are going to add
06:38 the previous class.
06:39 What's going to happen now is the Current class is going to come off, Previous
06:44 is going to go on, so it's going to drop down one order in the z-index.
06:49 We are going to tell the Next Photo that we are going to set its CSS property,
06:55 and make its Opacity 0, because that's what we are going to animate up.
07:01 The div will start out invisible.
07:03 Then we are going to set the Next Photo to be the current photo.
07:12 So now this guy is going to move on top and remember he is invisible.
07:15 So his Opacity is 0, he is now on top.
07:17 But now we need to animate it up from Opacity 0 to Opacity 1.0.
07:22 So it will fade up in front of the image that's behind it.
07:25 So we will write Animate, and we are going to write a Custom Animation function here.
07:31 The Animation function will simply animate the Opacity up to 1.0 over a duration
07:41 of 1 second, and then we are going to have a Call Back function.
07:46 The Call Back function will simply tell the current photo to remove its
07:57 previous class.
07:59 So now, there is no more class set on it.
08:01 It's back at the bottom of the stack along with the other photos.
08:06 So that is pretty much all we need to do.
08:09 So just a quick review.
08:10 We've found out which photo is current, we get the one that's next in line.
08:15 If we are at the end of the list, we move back to the beginning.
08:18 Whichever photo is current, we remove the current class and make it drop down in the z-index.
08:24 Then we take whatever is next, set it to be invisible, and make it on top.
08:30 Now that it's on top, all we have to do is animate the Opacity up to fully
08:33 visible over a second, so it will appear to fade up in front of the image that
08:37 it's in front of and then we tell the photo that was directly behind the next
08:43 photo to drop down at the bottom of the z-index.
08:47 Since we are calling set interval, this function will be called every two seconds.
08:52 So this will happen over and over and over again.
08:54 So let's save and what we are going to do now is go back out to the browser.
08:59 So let's bring up the imagerotator_ start in the browser, and you can see that
09:04 every two seconds, we are just changing which photo is on top and animating the Opacity up.
09:12 That's how you build an Image Rotator with some smooth animation using
09:16 jQuery and JavaScript.
09:19
Collapse this transcript
6. Using the jQuery UI Plug-In
Introduction to jQuery UI
00:00In the chapter we're going to go look at the jQuery UI library, which is a
00:05set of prebuilt functionality including things like effects and widgets
00:10and themes that you can use to give your web applications a polished, professional look.
00:15jQuery UI is a companion library that goes along with the jQuery library itself. Now, it's optional.
00:23You don't need to use jQuery UI if you don't want to, but it does provide a
00:28lot of really great features and functions that you can use in your web applications.
00:32Okay, so let's begin by taking to look here at the jquerui.com website, and a
00:38couple of things stand out right away.
00:41First you'll notice that it's laid out pretty much the same as the jquery.com
00:46website. You have the navigation bar up here.
00:48You got Demos, Download, API Docs, and so on. So layout is pretty much the same.
00:53Over here on the right-hand side, there is the Download section, and there is
00:56different ways to download jQuery UI.
00:59But first I want to focus over here on the left-hand side. You can see that
01:03jQuery is divided up into three sections.
01:07There's the Interactions section.
01:09There's the Widgets section, and if we scroll down little bit more, there is
01:14the Effects section.
01:15There's also the small Utilities section, but this is mostly used internally in
01:20the library, and I'm not going to go into it here.
01:23So let's scroll back up. In the Interactions section,
01:26You can think of these as some of the same effects that jQuery provides in its
01:31base library, only expanded somewhat.
01:33So, for example there are behaviors like Draggable and Droppable and Resizable.
01:38These are behaviors you can add to your own page elements.
01:41So for example you can use the Resizable Interaction to make something on your
01:45page resizable or you can use the Draggable to make something able to be
01:50dragged around on your page.
01:53In the Widgets section, there are a whole bunch of user interface widgets that you can
01:58use in your applications. There are things like the Accordion and then Autocomplete
02:02box and Datepickers and Progressbar, and a whole bunch of things you can use in
02:07your web apps to provide a really nice UI.
02:09Down here in the Effect section, now some of these are similar to the ones that are
02:14already in jQuery. You might be looking at this and saying, well, there's not a
02:17whole lot here that's new.
02:19Color animation is something that jQuery does not provide out of the box, and
02:23so these are the ones--the Hide and the Show--these are actually greatly
02:27expanded in jQuery UI.
02:31A couple of other things I want to point out before we go into the rest of the chapter.
02:33First let's scroll back up over here. Under the Developer Links there's a couple
02:39of links I want to point out that are really useful.
02:41First there's the Development Planning Wiki, and if we visit the Development
02:45Planning Wiki, you can see that this is a web page that explains the jQuery
02:50UI planning process.
02:52You can use this to keep track of all the different things that are going on in
02:56the Development of jQuery UI. It lists thing like events and various planning and
03:00developing status of what they are up to.
03:02And if we go back to the main page, one other thing to point out is the Roadmap.
03:06Roadmap gives a detailed assessment of what is going on in jQuery UI
03:11Development, and so they list out the upcoming releases.
03:13You can see here, there is the 1.11 release.
03:16There is 1.12 and so on, down here.
03:18They have got the 2.0 release coming up, and they're listing out all the
03:22different things that they're working on for upcoming releases.
03:25If jQuery UI doesn't provide what you need today, you can visit this Roadmap to
03:30see what is that they're working on.
03:31So now that we have an overview of what jQuery UI is, let's jump in and start
03:37exercising library and see what it can do.
Collapse this transcript
Exploring the jQuery UI widgets
00:00Let's dive in and take a look at some of the widgets that jQuery UI provides for
00:05us to use in our web applications.
00:07So, I am going to scroll down, and I'll start off by looking at the Accordion widget.
00:13Now, before I go into the demo for this particular widget, I want to point out
00:17how the demo pages are laid out here on jqueryui.com.
00:21So, right here in the middle is the widget that's being demonstrated, and then
00:26over here on the right-hand side there are various options you can set for that
00:30particular widget, under the Examples section.
00:32If we scroll down a little bit more, you can see that there is a view source
00:36option, and if I click on view source, then the source code that was used to
00:40build this particular example right here is listed out.
00:44When you download jQuery UI, all of the source code for the demos comes down
00:48with the download anyway.
00:50So, you can either choose to use that sample code or you can copy and paste from
00:53here on the website. All right! Let me go ahead and close the view source.
00:58Last thing I want to point out is the API documentation. Each one of the widgets
01:02has a link to the API documentation for that widget.
01:05So you can see here that this is the page that explains all the various things
01:09that the Accordion widget can do, explains in detail all the different options,
01:15how to call it with various different properties, and so on. All right!
01:19So let's scroll back up, and let's go back to the Demo.
01:24So, Accordion widgets, I'm sure you have seen these before, they are used to
01:28break up information into easily expandable sections, right?
01:31So, I click on each section and then it gets slowly revealed into place.
01:35But of course there are various options I can use. For example I can
01:38customize the icons.
01:40I can have the Accordion widget open just by hovering instead of clicking, and
01:44so on, and so forth.
01:46We'll actually use the Accordion widget a little bit later when we build our real site.
01:51One of the things I want to point out really quickly though, is that to use many
01:53of these widgets is really pretty simple.
01:56Let's go down to the view source, and you can see here that just to use the
02:02Accordion widget, all you need to do is call the accordion function and pass in
02:06whatever parameters you want to use on the ID of the element that you want to be the accordion.
02:12If we go down to the source code a little bit further, you'll see that
02:16here's the div with the ID of the accordion on it, and it follows some
02:20internal structure, right?
02:21These h3s provide the titles.
02:23These divs provide the content, and so on.
02:26But all you have to do is set up your content like you normally would anyway and
02:30then just call the accordion function and off it goes.
02:33So, next is the Autocomplete widget.
02:35Autocomplete allows me to have a text field.
02:38When I type certain letters, you can see that I'm getting a whole bunch of
02:43predefined options that have that letter in it.
02:46So, let's check off the A and type a J instead.
02:49You can see that I'm getting programming languages that just have the letter J
02:54in them. And I can get more specific if I start doing things like this.
02:57And once again, the Autocomplete box has a whole bunch of different options over
03:03here on the right-hand side. I can have it show up as a Combo box.
03:06I can have various categories.
03:09So if I type a little a again, you can see that there is Categories in here that you can't select.
03:13A pretty rich set of functionality for an Autocomplete wwidget.
03:16jQuery UI also provides buttons.
03:19Now here are the pretty standard simple buttons, but you can of course have
03:22things like checkboxes.
03:23Here they are, behaving like checkboxes.
03:26You can assign icons to your buttons. You can have them behave like Radio
03:30button. You can have a Split button, which has various different actions here.
03:35You can build toolbars out of buttons, pretty rich functionality.
03:38Than there is a Datepicker widget.
03:40I am sure you have seen these before. It allows you to choose a date.
03:43All right, pretty straightforward, you can have multiple dates.
03:47Here is Display multiple months, right here. So you can imagine if you are
03:51checking into a hotel or choosing plane flights and so on, this would be very useful.
03:55You can even have them be localized.
03:57So if I choose Localization, you can see that here's the Calendar in French.
04:01We can also use various different writing formats.
04:04You know, here's one in a right-to-left languages, using Hebrew. Okay, let's move on.
04:09Here's the Dialog widget.
04:10I am sure you have seen dialogs before, nothing really earth shattering here.
04:14But there's a Basic dialog.
04:15There is Modal dialogs, and then you can have Modal messages and so on.
04:19These are pretty useful in modern web apps.
04:22There is a new Menu element here in jQuery UI.
04:26Now they are still working on the Menu bar as of this recording, but this is the
04:30Menu element, and you can see, you can customize it with icons.
04:33Here is the Progressbar. Progressbar of course, we've all seen these before.
04:38You can have them with various labels and indeterminate value and so on.
04:42This is the Slider control, again very useful control. Here is one with
04:47multiple sliders, all right. Here's a Range slider.
04:51You can see that this is pretty useful.
04:53You can imagine like you know choosing a range of hotel room prices you want to pay.
04:57Down here is the Spinner control.
04:59The spinner control is pretty straightforward. It just allows you to use these up and
05:02down arrows to quickly choose numbers.
05:04There is the Tabs control.
05:08Again, I'm sure we've all seen tabs before. And there's the Tooltip control.
05:14So that's a pretty brief recap of the widgets available in jQuery UI.
05:21So, let's move on to our next section.
Collapse this transcript
Exploring the jQuery UI effects
00:00Okay, now let's take a look at the interactions and effects in jQuery UI.
00:05Now remember interactions and effects are not widgets. You don't add these to
00:09your page; what you do is you apply these behaviors to things that are
00:13already in your page.
00:14So, let's begin by looking at the Interactions section, and I am going to
00:18click on Draggable.
00:19Now I want to point out again that the way the pages laid out is very similar to
00:24the layout for widgets.
00:25So, here's the example.
00:27Over here on the right-hand side are various options you can try out.
00:30If you scroll down, there's a view source option, and of course there's a link
00:35to the API documentation.
00:37So, beginning with the Draggable interaction, you can see that when I click on
00:42this div, I can drag it around, all right.
00:44Now this is a div I already have in my page and I've simply applied the
00:49interaction of being draggable to it.
00:52And I can also do things like constrain movements. So for example, this one can
00:56only be dragged vertically. This one can only be dragged horizontally. This one
01:00over here is only draggable within its parent.
01:04This one here is within this containing box right here.
01:09There are various different things you can do.
01:11You can have handles.
01:12Here's one that can only be dragged by its handle. When you click somewhere
01:15else, you can't drag it. All right!
01:17You can drag this around, but you can't drag by this handle for example.
01:20So, Draggable is a behavior that you can apply to things that are in your page.
01:26Let's take a look at another example. There is Resizable.
01:29We're going to be using Resizable in our real-life website, when we build it
01:36later in the course.
01:37So, again you can see that Resizable, when the Resizable interaction is applied
01:42to an element, this little resize handle appears down in the bottom of the element,
01:47and I can just drag on that and resize it.
01:51I can also do things like constrain the resize area.
01:54So for example, I can't resize outside this parent.
01:58I can have a maximum and minimum size, so I can't make it any bigger than that.
02:02I can't make it any smaller than that.
02:04There are different things
02:06I can, do things like animations. So I can drag out a little ghost image, and
02:10then it grows into my resizable area.
02:13Again, you get the idea.
02:14I am not going to go through all these, because they're pretty exhaustive, but
02:18the idea is that these are behaviors that you apply to things in your page.
02:21They're not predefined user interface, the way that things look; you simply
02:25say, I want to make something resizable or draggable or droppable and then you
02:30apply the jQuery UI behavior just like you would apply any other kind of
02:35behavior in your code.
02:36Let's scroll down over to the effects.
02:38Let's take a look at the Color Animation effect.
02:43So, I am going to go ahead and toggle the effect, and you can see that jQuery
02:47UI provides a way of animating not just things like size and position, but
02:52animating color, and this is something that jQuery itself does not provide out of the-box.
02:56So, if you want to animate between two colors, you can use the Color Animation effect.
03:01There are also various other effects you can use in jQuery UI.
03:04Let's take a look at the Show effect.
03:07Now again, jQuery itself provides effects for things like showing and hiding
03:11and fading and so on, but you can see here that jQuery UI provides a whole
03:15bunch of other effects.
03:16So, for example there's the Blinds effect, where it comes in as if you're
03:20dropping blinds. Here's the Bounce effect.
03:24It bounces into place. Here's the Explode effect, which kind of explodes into place.
03:30Here's the Pulsate effect. You get the idea.
03:33There's a whole bunch of prebuilt effects that you can use in your pages.
03:38And so once again, if we click on the view source, you'll see that using the
03:42effects is really very simple.
03:44You simply use the Show method with the effect that you want to have applied to the element.
03:50So, just like widgets, you can use interactions and effects to apply really
03:54professional-looking UI to your web pages.
Collapse this transcript
Using the jQuery UI ThemeRoller
00:00Now, you might be wondering if it's possible to customize the appearance of the
00:04widgets provided by jQuery UI.
00:06You probably have your own custom CSS and appearance in your web application, and
00:10you want to make sure that the widgets that you're going to use from jQuery UI
00:14look the same inside your web page.
00:17And the answer of course is yes.
00:20You can customize the way that they look.
00:21That's what we're going to do in this movie.
00:24So, let's go ahead and click on the Themes link right here.
00:27That will take me to the ThemeRoller application, and here is where I can
00:32customize the way that the widgets look.
00:34Now, before I get started, let me just point out that this page, the ThemeRoller
00:38application, contains all of the user interface elements that jQuery UI provides.
00:44So we can see, at a glance, how each thing is going to look when we change the appearance.
00:49Right, here's the Accordion, the Buttons, the Autocomplete, the Spinner, the
00:53Datepicker, the Tabs and so on. So all the controls are right here on one page.
00:57Now, there's two ways to use ThemeRoller to build a custom appearance.
01:03The first way is to simply roll your own, in which case you have a whole bunch of
01:07categories that you can expand out and change the various settings for the theme
01:11that you can then download.
01:13The other way to do it is to use the gallery of prebuilt themes that you can
01:18apply to the various user interface elements.
01:21So let me begin by just choosing a couple of the prebuilt themes so you can see
01:26what they look like.
01:27So, you can see here when I click on UI lightness, I get a nice prebuilt look
01:33and feel for these components.
01:36If I click on UI darkness, I get a completely different look and feel, and if
01:40I scroll down here to the bottom of the page, you'll see that below the section over here,
01:44there is a Reverse page background color option.
01:47So, I can click that to see what the theme will look like on a dark-themed page.
01:52And you can see in this case the UI darkness looks a lot better on a dark theme
01:57page than it does on a light theme page.
02:00So, let's try a few more.
02:01Here's the Smoothness theme.
02:03Here's is the Start theme.
02:04Here is the Sunny theme. In fact let me go back and turn of that setting
02:09to go back to the light-colored page.
02:11Let's scroll back up. And this one, the Sunny one, looks kind of nice, and you
02:15know what? I think I am going to use this one in my practical example for later.
02:19Now, one of the other things I want to point out is that you can customize the
02:24way that any one of these prebuilt themes looks by going back over to the Roll
02:30Your Own category after selecting the one that you want to use.
02:35So, in this case, if I go back to Roll Your Own, you'll see that now these
02:38categories have picked up all the settings that are predefined by default in
02:43the Sunny category.
02:44So, let me go scroll down a little bit and show you how some of these
02:48changes might look.
02:49So here's the Alert Sample, right. This is what an error message might look like
02:54using the Sunny UI sample.
02:55So, let me scroll back up and choose the Error category.
02:59Let me choose a different background color.
03:01So right now it's kind of this reddish kind of color.
03:04Let me make it purple, okay, and you can see that when I scroll back down now,
03:09you can see there it's changed.
03:11I'll scroll back up. And let me choose a different icon color.
03:15Let's make the icon nice and green, and when we scroll back down, you can see
03:19that the icon is now green.
03:21So, you can customize the way that any one of the prebuilt themes looks inside
03:26of the ThemeRoller application.
03:28Now, when you're ready to download, you can download the theme just by itself.
03:33If you click on Download Theme, this will take me to the Download Builder
03:36application, which will allow me to download all of jQuery UI as well as the theme.
03:42But if I scroll down, you'll see that this has now been set to Custom Theme.
03:46It lets me choose the name of the folder.
03:50I can rename it here if I want to.
03:52So, I could rename it Joe's Sunny theme or whatever.
03:56Let me go back to the ThemeRoller application. All right!
04:00So that's how we use ThemeRoller to build a custom appearance or a predefined
04:06appearance for the widgets that you want to use in your application using
04:10jQuery UI.
Collapse this transcript
Downloading and installing jQuery UI
00:00Now to use jQuery UI in your project you of course have to download it and
00:04include it in your application, and that's what I'm going to cover here.
00:08So here I am on the jQuery UI homepage, and there's a couple of different ways
00:13to download jQuery UI.
00:15There is the Custom Download, which I'll get to in just a second, but there is
00:19these two other links down here.
00:20There is the Stable quick download which gives you a ZIP file of the current
00:26jQuery UI Library, and at the time of this recording that's version 1.10.2, which
00:32you need to use with jQuery 1.6 or later.
00:36There is also the Legacy download, which is a version back, and that's 1.9.2, and
00:41again you can see that requires at least jQuery 1.6.
00:45So if you just want to download the quick, you know, default widgets in their
00:49current state, just use one of these two links.
00:52I'm going to, however, show the Custom Download, which is this link right here.
00:56When I click on the Custom Download button I get taken to the Download
01:01Builder, and the Download Builder allows me to customize what I want to
01:05include in my jQuery UI download.
01:08Again, up here I've got the various stable and legacy links, if I change my mind
01:14I want to just download those, or I can scroll through here and see the various
01:19pieces of jQuery UI.
01:22So I can choose to only include the ones that I know I'm going to be using.
01:26I'm going to choose to leave all of the core options selected because these are
01:31the pieces of jQuery UI that you're going to need in order to use pretty much
01:36any of the basic functionality.
01:37But I can do things like turn off various functions that I know I'm not going to use.
01:42So for example, if I know I'm not going to using the Sortable or Selectable
01:45behaviors from the Interactions, I can just turn those off.
01:48Similarly, I can turn off any of the controls that I know I'm not going to use.
01:53So if I know I won't be using the Progressbar and the Slider and the Spinner and
01:55so on, I can just go ahead and choose to turn those off.
02:00Now I'm going to leave them all clicked for the sake of argument. And then down
02:05here, you get the idea,
02:06I can do the same thing with the effects. I can just choose not to use
02:10whatever effects I know I'm not going to use.
02:11And then down here at the bottom is the CSS Theme that I want to include with my download.
02:18Now, if I came here straight from Download Builder on the homepage, I can
02:22choose any one of the default ones. So for example I can just choose to
02:27download the Sunny theme.
02:28If I chose to come here through the download link on the theme roller page, I
02:33would have the option of choosing the custom theme that I just built, along with
02:37all of the options I just showed you, turning off the interactions and the
02:40widgets and the effects and so on.
02:42So what I'm going to do is just choose one of the basic CSS pre-provided ones,
02:47in this case the Sunny theme, and I'm going to choose to leave all of these guys
02:52clicked, so that you can see what the resulting folder looks like.
02:55Then I would just choose the Download button right here, and that would download
02:59the jQuery UI with all of selected options to my computer.
03:03Now I'm not going to do that right now because it takes a few minutes and I've
03:07already done it anyway, so let's go over to the Desktop and take a look at the
03:10folder and see what it looks like when it comes down to your computer.
03:15So here on the desktop you can see that after I've downloaded jQuery UI,
03:19I get this ZIP file, which is jQuery-ui-1 .10.custom.zip, and I've extracted that
03:25to this folder right here.
03:27So let's go ahead and open the folder, and inside the folder is another folder
03:31that contains all of the jQuery UI stuff. And it's organized into three different folders.
03:37There is the js folder, the css folder, and the development-bundle, and I'm
03:41going to take a look at each one of these.
03:43Now, before I look at the folders, let's double-click on the index file and
03:46see what's in there.
03:48Now the index file shows what my components are.
03:50Now this shows all the components because I chose to download all of them, but
03:54this would show me all of the components that I chose to download in the theme
03:58that I chose to download them with.
03:59So you can see that as I scroll through here, I've got all my UI widgets that
04:05are using the base preprovided Sunny CSS theme.
04:10This gives me a quick glance at the existing components that I chose to download.
04:15All right, so let's minimize this.
04:17Let's take a look at the js folder.
04:20Inside the js folder I get two versions of jQuery UI.
04:25I get jQuery UI 1.10.2, or you know whatever the current version is that you downloaded.
04:31This is the uncompressed development version, and here is the custom.min.js, and
04:37you can see the difference between this two file sizes about 200K.
04:41Just like in regular jQuery, you have an uncompressed development version that
04:46you can include in your web page while you're developing, and then you have a
04:50compressed version that you can include on your production website which takes
04:54less space and less time to download.
04:55jQuery UI also includes the latest version of the jQuery Library itself.
05:01Now, that's the uncompressed development version.
05:04But you can choose to use the compressed version if you want to.
05:07All right, let's go back up.
05:08Now, let's take a look at the CSS folder.
05:11The CSS folder contains the name of the theme that I chose to download, and in
05:15this case it's the Sunny theme.
05:17So if we look inside here, you'll see that this is the CSS code, and again I get
05:21two different version. I get the uncompressed development version and the
05:26compressed and minified production version. And this is what I would put up to my
05:30production web server. And then inside this folder here are all of the images
05:34that that CSS theme needs in order to use.
05:37Let's just go back.
05:38So this is what you would put into your CSS folder, or you could just choose to
05:44include the top-level folder and write the paths in your HTML files
05:49appropriately to go into this folder in order to access your CSS.
05:54Now, let's take a look at the development bundle.
05:56Now, the development bundle folder comes down by default, and it contains all of
06:01the developer information that needs to go along with building jQuery UI.
06:06Unless you're interested in seeing how the developers work on jQuery UI or
06:11contributing to the source code,
06:12you probably won't ever need to use what's in this folder, except for two, and I'm
06:16going to point those out right here.
06:18The first is the docs folder.
06:20Now, the docs folder contains the documentation for all the various widgets.
06:24So if you happen to be working offline and you don't have access to
06:27the Internet, you can come in here and see the documentation for the various widgets.
06:32So let's go ahead and click on the Accordion widget and bring that up in the
06:35browser, and you can see that it's pretty barebones, right?
06:38I can click on these various links and you know read the documentation that goes
06:42along with the widget itself.
06:45This is true for all of the stuff I downloaded.
06:48Let's go back to the folder.
06:49All right, let's go back up one level.
06:52Inside the demos folder are demos for each and every one of the components that
06:58I downloaded. So once again I can go into the accordion folder and I can click
07:03on the index file that's in that folder and bring it up and now all the demos
07:07that I had online on the jQuery UI website itself are here locally on my
07:12machine. So for example, I can click on the default functionality link, right, and
07:17you can see that this is the demo.
07:18I can go back, choose the Open on hover.
07:22So you can see that these are all the demos that are available online, and I now
07:26have them locally on my machine.
07:29So I can debug them and see how they work and otherwise use them.
07:33All right, so let's go back to the folder.
07:35just to recap, in order to deploy jQuery UI, what you would do is, inside the
07:40JavaScript folder, you would choose which one of these two files, either the
07:45uncompressed or the compressed version, put that in your scripts folder along
07:49with whatever library of jQuery that you're using, in this case it's 1.9, and
07:54then back up here in the CSS folder,
07:58here in the CSS folder, you would either copy this parent folder or these
08:03components of the theme and put them into your CSS folder and then reference
08:08them like you would any other CSS that you're using in your project.
08:12And we'll see how to do that when we build our practical site later on in the course.
Collapse this transcript
7. Putting It All Together
Overview of the sample web site
00:00 So we have reached the point in the course now where we can take what we have
00:04 learned about jQuery and put it all together into a real site.
00:08 What we are going to do is take a site that we have built that was built without
00:13 jQuery and we are going to update it with some new features to make it more Web
00:18 2.0, more interactive, a little bit more visually appealing.
00:22 We are going to do a few things.
00:23 First, drawing on our jQuery UI widget knowledge,
00:27 we are going to go and add an Accordion control to our website and see how that works.
00:33 Then we will build an image rotator that has some nice smooth transition effects
00:39 and we will do that just using jQuery in CSS.
00:41 We will also build some mouse over tooltips and we will build a
00:46 jQuery-based image selector.
00:49 So you will click on some thumbnails and that changes a larger view into the
00:53 current image and then we will use one of the jQuery UI interactions to make a
00:58 form field resizable and constrained.
01:02 So let's begin by taking a look at the current site the way that it is and then
01:07 we will take a look at the site after it has been modified to work with jQuery.
01:13 Groundswell is a fictitious surfing shop site and it has got all kinds of stuff on it.
01:20 So here you can see that there is a news section and we got different photos and
01:25 you can buy some products and that kind of stuff. So let's see.
01:31 That's the homepage and if we go to Gear section, you can see that they have got
01:35 some stuff that's for sale.
01:37 We click on the Boards and it looks like in this section we have got boards that
01:42 are listed here and over here I have got the current board that I am looking at
01:46 and if I click on the view larger link, you will see that that takes me to a
01:51 separate page where I can see the larger graphic image. Then let me see.
01:56 Go to the Lifestyle page.
01:58 There is a Photo array that's pretty standard.
02:02 There is also a Register link where I can register for the site and you can see
02:06 I can fill out a form. Pretty good.
02:09 Let's take a look at the site however once it has been updated to work with jQuery.
02:13 So let's go back to the folder and we will go to the Final version and we will
02:19 bring this guy up in the browser.
02:21 So this is pretty similar, but a couple of things right away you notice off the bat.
02:25 Over here now, this image array is being animated and it has got a nice little
02:29 fade effect going on and that's all done with jQuery and CSS.
02:32 There is no plug-in there.
02:34 You can see that the News Section has been updated to use the Accordion control
02:39 that we saw in the jQuery UI chapter.
02:42 So I can now click on these various Accordions and read the news, and that looks a lot nicer.
02:48 It's a lot more interactive.
02:49 It's nice and smooth, scrolling and animated.
02:52 So there are some changes to the Homepage that have been made.
02:55 Let's go check out the Gear section.
02:57 So here is the Gear section.
02:59 Let's go look at the Boards.
03:01 Now you can see that here in the Board section rather than having to click on
03:06 the view larger link, I can just roll over it and you see the nice little
03:10 tooltip fades in and fades out.
03:14 So that's a lot nicer than having to go to a separate page.
03:18 What other changes have we made?
03:20 So here in Lifestyle section, if you look at the Register, you can see that
03:25 the Comments field is still there, but now you got a nice little resize handle down here.
03:30 So I can resize this up and down based upon how much room I need to enter my comments.
03:38 So all of this has been accomplished using jQuery and we are going to go look
03:42 now at how each one of these features was implemented, so you can implement
03:47 these features and others in your own sites.
03:50
Collapse this transcript
Using the accordion widget
00:00Let's begin by adding the accordion control to the index page of our website.
00:07So, what I am going to do is open the index page in my Editor.
00:13Let's scroll down to the Latest News section and that's this guy right here.
00:19So let's find that in the source code.
00:21Recall the accordion control needs to have a single container that it uses to
00:27encapsulate all of the content tabs for its content.
00:31So, what I am going to do first is wrap the contents of the News section inside a div.
00:39This now gives us one container to point the accordion control at and say yeah,
00:43this is what you need to use as your container.
00:45So I am going to give it an id and the id is what we are going use to identify it
00:50 as the accordion control and I am going to give it an id of newsSection.
00:58Now that I have created div, I am going to take the other part of it, and scroll
01:03down to the end of the newsSection and put it in right there.
01:08That's the first thing I need to do.
01:10So, now I have wrapped in a div. Now, include the jQuery library and
01:17its associated jQuery UI library.
01:19So, we are going to go back up to the head section, and let's quickly just look
01:23at the folder structure.
01:26So, here in the folder, let's look in scripts and you can see that there is
01:29jQuery and I am also using the minified version of jQuery ui.
01:33So, these are the production version script files that I have downloaded from the site.
01:39So, let's go back into the code.
01:42So, a couple of things that you need to do first.
01:43First, I am going to include a script and that's going to be the jQuery file.
01:53So, that is located in the scripts folder and recall in the scripts folder,
02:01I am using the minified version of the jQuery library.
02:04So, all I need to do is name it correctly.
02:07That's jquery-1.3.2.min.js, and that's all I need to do there.
02:15So, now I need to include the script for jQuery ui as well.
02:19So, that's also script of type text/ javascript and that one is located in
02:26scripts folder too, scripts and that is jquery-ui-1.7.2.custom.min.js.
02:44So, now I have included my scripts and I need to include the link to jQuery ui
02:50style sheet as well.
02:51Just remember when we rolled up that theme and downloaded it, we need to include that.
02:56So, it's going to copy this link and type in a reference to the style sheet.
03:03Let's makes sure we have the name correct so let's pop-out to the folder and
03:08let's look in the CSS part and that's the sunny theme and
03:16it's jquery-ui-1.7.2.custom.css
03:20Okay, so that is in css/sunny and it is the jquery-ui-1.7.2.custom.css.
03:36So, now it's time to write the script that's going to create the accordion control.
03:41So, I am just going to type script type is text/javascript.
03:48We want this to happen when the document loads.
03:50I am going to type a little jQuery code here.
03:54Since I want this to happen on the document load.
03:56Throughout the course, what I have been doing is writing a document ready event,
04:00right. I've been doing something that looks like this and that's all fine and good.
04:04There's actually a shortcut, however, that you can use for the document ready
04:08event and that is just to simply pass a function to the jQuery object.
04:12So, I am just going to write a dollar sign and then brace and then function.
04:18And this is the function that's going to be called when the document loads.
04:22And all I need to do is tell jQuery to get me the newsSection item, remember
04:29that's the div that we created, and then all I need to do is type accordion.
04:36Remember, however, that the accordion control by default looks for link tags
04:41as its header and paragraph as its content.
04:45We need to make a little fix there because you see these are h4. These are not links.
04:48The paragraphs are fine, but these are h4 tags.
04:51So, let's scroll back up and type in that little option here, because the
04:56accordion control gives us the option of what to specify as the header.
04:59So I will pass in an object that says that the header is an h4 and that should fix it.
05:06So, now we are ready to test out the page, and make sure that that's working. So, let's do that.
05:12Let's go to the browser.
05:13I am going to just click on index.htm, bring it up, and let's scroll down.
05:18And you can see that the accordion control is there but something is off.
05:22So, we had to fix the CSS Style Sheet, and this is a common issue that you will
05:26run into when you are updating your existing site.
05:29You can see that the control works, but the CSS is clearly off.
05:33We got to go and fix that.
05:34So let's go make some changes to our CSS codes so that the title looks okay and
05:39everything is all nice and cleaned up.
05:41So, we will switch back to the code.
05:44So, let's go into our CSS file and fix this.
05:49Here is our CSS file.
05:50So, there's a couple of quick changes we need to make to the CSS code.
05:53What we are going to do is define a couple of style sheets for our content.
05:58So, let's scroll down to where the main content is.
06:02Here is the main content section and in the main content section, we are
06:06going to define a couple of style sheets that are particular to our page to
06:11make the h4s work correctly.
06:13So what we will do is we will say #mainContent.
06:17We are going to create a style sheet here for h4, and the span.subhead inside
06:24the h4 because that, if we look back at the source code, and we scroll down
06:30to the newsSection, you can see that there is some spans in here and we need to
06:35fix some offsets that the accordion control is not liking.
06:39So, what we are going to do is inside the subhead span, we are going to define
06:43a margin-left of 20 pixels, so that everything is offset from the
06:50left-hand side of the page.
06:52Then we are going to create a style sheet for the news section itself and we
06:56will call that newsSection and inside the newsSection, we are going to assign a
07:04margin-left of 20 pixels.
07:10And for the newsSection's h4 and the newsSection paragraph tags, we are going to
07:23define the margin-left to be 0, and the margin-bottom to be 0.
07:33In fact, actually this probably needs more like 40 pixels.
07:38So, now let's go back and see what changes that has made.
07:42So, we are going to go back to the browser and we are going to refresh and
07:49you can see that that's pretty much cleaned everything up.
07:51So, what I have done is I have gone through and it looks like one little guy
07:55did not make it in.
07:55Let's go back to it and see why that is. Let's see.
08:00It looks like this guy is missing a span.
08:04So, we need to copy that span right there, we will put that span around him, and
08:13go back and we will go back and refresh and now everything is fixed.
08:16So, now you can see that the text fits just fine and the accordion control is working.
08:20So, that's pretty much all we needed to do to get the accordion control
08:24integrated into the page and you will find if you have existing sites, you may
08:28need to mess around with your CSS a little bit to get it to accommodate the CSS
08:32that the controls we are going to use.
08:34In that case, it's pretty much as an exercise in figuring out why the elements
08:39are positioned wrong.
08:40And usually just by tracing through the CSS you can almost immediately find out
08:43what's going on or you can use whatever built-in browser tools you have in
08:47browsers like Firefox and IE now provide tools for looking at what CSS is being calculated
08:53for given elements.
08:54So, we have pretty much built the accordion control and it's working.
08:57Let's make sure that it works in Firefox as well.
08:59Let's go back out to the folder.
09:01Here we are in the folder, let's open this up with Firefox and you can see here,
09:09yeah, it's working just fine.
09:09It is the same accordion control, same page, same behavior.
Collapse this transcript
Creating an image rotator
00:00So now that we have the accordion working, let's go build our image rotator.
00:06That's going to cause this image right here to display a nice smooth slide show
00:11through a series of images.
00:13Let's look in our folder really quick.
00:16So here we are in the folder and you can see inside the _images folder, we have
00:21a bunch of thumbnails of the photos we want to flip through.
00:26So you can see that each one has the name thumb on the end, and there is four of them.
00:31So let's go back to our code.
00:36In the code, in the sidebar, that's this image right here.
00:40So that's the image that's currently there.
00:42What we want to do however is build a slide show around this image.
00:47So let's clean this code up a little bit right here and isolate that image.
00:54What I am going to do is put a div around this.
01:00And that's going to serve as our photo show, in fact, I am going to give it an
01:04id called photoShow.
01:07What I am going to do now is copy and paste these images right here, so I have
01:11got the four images that we are going to loop through and each one of them is
01:14numbered in ascending order.
01:17So that's 2, that's 3, and that's 4.
01:20And for the alt, I am going to change it from that to be Photo Gallery.
01:27And I am going to do that for each one of these guys, so that we got valid
01:30alt text on each one. Let's see.
01:35We are going to leave everything else pretty much the same.
01:39I also want to do something else, I want to make sure that each one of these
01:41images is clickable so that when the user clicks on the image, they go to the
01:47photo's home page of the lifestyle section.
01:50So I am going to put in link and I am going to say a
01:58href="lifestyle/photos.htm", and let's see that should be about it.
02:07And I am going to take off that thing there, put on to the image.
02:12So now I am going to do that for each one of these guys.
02:16Copy, paste, paste, paste and the closing links.
02:24So now I have got links around each one of the images, so what I need to do now
02:28is build the rotator.
02:30And to do that, before I actually build the code for the rotator, what I am
02:34going to do is define some CSS that will help me.
02:38Now the CSS that I am going to write is going to do a couple of things.
02:42It's going to position the images so that they are all underneath each other,
02:46and I am gong to define some z-index information so that the images appear to
02:51bubble up from underneath each other.
02:54Let's go to the sidebar section, and I believe the sidebar is down here somewhere.
02:59So here we are in the sidebar section, so what I am going to do is define a
03:03couple of style sheets, go a little down here.
03:06So down here, the first thing I am going to do is define a style for my photoShow div.
03:11So my photoShow is going to have a style sheet that does a couple of things.
03:17First, I am going to set the position to be relative.
03:22Then I am going to define a height and a width that encapsulates the images.
03:27So the height is going to be 190 pixels because that's how big the images are,
03:32and the width will be 240 pixels because that's how wide the images are.
03:38And then I am going to set the margin left to be auto, this way it correctly
03:44positions itself inside that sidebar div.
03:48Now that I have defined a style sheet for the containing div, I am going to
03:53define another style sheet, this time it's going to be for the div that's
03:56inside the photoShow.
03:59Remember, this encapsulates all the images, and each image now is going to be
04:04encapsulated in a div as well.
04:06I am going to write that the position is absolute, and I am going to set the
04:14z-index of each image to be 0.
04:17Let's go back to the code.
04:20So here's the div for the photoShow and what I am going to do is I am going to
04:24wrap each link and image pair inside a separate div.
04:29Because these divs are going to be what we are hiding and showing to the user.
04:33So I am going to do it for that one, and I am going to do it for that one, and
04:38for that one, and for that one.
04:40And I will close each one off correctly.
04:43There is that one, there is that one, and there is that one.
04:48Now let's go back to the CSS.
04:50So now I have defined style sheets for both the containing divs and for the divs
04:54that wrap each one of the images.
04:56So I am going to write a couple more style sheets now that help me position
04:59the images properly, so when they are animated they show up correctly in the stack order.
05:04Right now it's photoShow, and this is going to be for the div that I am going to
05:10give a class of previous.
05:15And for the previous one, I am just going to give that a z-index, index of 1.
05:22And for the same thing I am going to call this the current image and that's
05:28going to have a z-index of 2.
05:29So what this is going to do is cause whichever image has the current style sheet
05:33to appear on top of all the others, and the one that has previous is going to
05:36appear directly underneath it.
05:38So all I need to do now is make sure that the first image starts off with the
05:42correct style sheet applied.
05:45So let's go back to the code and we'll give this guy here, the class of current.
05:52He is going to start out on top and all the other images will start out
05:57underneath that one.
05:59Now it's time to simply go write the animation code, but before we do that let's
06:03go out to the browser.
06:06And let's refresh, and you can see that so far there is no visible changes but
06:10there are four images there.
06:12And they are all stacked up underneath each other.
06:14So now let's write the rotation code.
06:15Okay, back to the code.
06:20So once again, we want this to happen on the document.ready, so I am just going
06:25to put that function in here as well.
06:26So let's write the code now that's going to cause each one of the images to rotate.
06:30What I am going to do is call the JavaScript setInterval() function and I am
06:36going to write a little routine called rotateImages, so this will be called.
06:42And let's call it every two seconds.
06:45So the value of 2000 is 2000 milliseconds which is 2 seconds.
06:49Now we need to write the function for rotateImages, and what we are going to do
06:57first is get the current photo.
07:01The current photo is going to be the result of a jQuery operation.
07:05That's going to get me the photoShow's current div.
07:12Okay, so that's going to get me whichever div has the current class.
07:17Then we are going to get the next photo, this is the photo that we are going to fade too.
07:24And that's going to be whatever the current photo's next, remember using our
07:31traversal knowledge from earlier in the course.
07:33This is going to get the next div that's in line.
07:36If we are at the last div, that's not going to be existent, so we need to catch that case.
07:40So if it turns out that the next photo's length and remember this tells us how
07:45many items were in that jQuery array.
07:48The length of that is 0, then we have reached the last div.
07:52So what we need to do then is say well, okay in that case, the next photo is
07:55equal to just the first photo.
07:58So we are drawing again on our jQuery knowledge, we are just going to go ahead
08:01and retrieve the photoShow's first child using the first filter right here.
08:10And this will get us the first div that's inside the photoShow.
08:12So now that we have got the right photos that we are going from and to, let's
08:17use a little statement chaining to do some animation.
08:21So we are going to tell the current photo, we are going to remove the current
08:26class from our CSS section and we are going to add a class, we are going to add previous.
08:36So this will cause whichever photo was current to drop down one level in the z-index order.
08:41Then we are going to tell the photo that's next that we are going to set its CSS
08:46property, we are going to set its opacity to 0.
08:53Then we are going to add a class, and we are going to make this one the current photo.
09:02And we are going to animate the opacity using our basic animation function.
09:07We are going to animate the opacity up to 1.0 over a duration of 1 second, and
09:17we want to declare a callback() function.
09:20Remember the callback() function feature of this.
09:26The callback() function will tell the current photo to remove its previous class.
09:35So that way the next photo after that will pop-up in the order.
09:39So after we have done that, we are ready to save, and now we should have our
09:47image rotator working.
09:49Let's go ahead and switch back out to the browser, let's refresh.
09:56So now you can see that every two seconds what's happening is each div is now
10:01animating its opacity and fading up in front of the image in front of it.
10:05So each image here, whichever one is current, drops down below the one that's next.
10:10The one that's next is set to opacity of 0, so it's practically invisible.
10:14And then it fades up in front of the image in front of it.
10:18That is the image rotator in jQuery.
Collapse this transcript
Building hover tooltips
00:00So, now let's go implement that tooltips feature that we saw, when we rolled
00:04over the surfboard and we show the user a large thumbnail of the board that they
00:10are looking at, rather than making them go to a separate webpage.
00:13I am going to go ahead and open up my file here in my Editor and let's go to the site.
00:23Remember that was in the gear section, and I am going up the boards file here.
00:28So, a couple of things I want to point out.
00:31Here is the webpage, and what I am going to do is scroll down to the bottom.
00:38So you can see down here at the bottom, there's a div here whose class has been
00:43set to tooltipContent, and the tooltipContent is, if we go look at our CSS.
00:52So, let's first find that.
00:57That tooltipContent div is positioned absolutely and set to have a display of
01:02none and then opacity of zero.
01:05So this is the large graphic that the user is going to see when we mouse over
01:11the view larger item.
01:14So if we scroll up here we will see that the view larger, that little Plus sign,
01:20is right here and you can see that has a link to the larger image page that
01:25opens in a new window, and it's got this id right here named viewlarger.
01:30So, what we are going to do is we want to write some script code that allows
01:36the user to mouse over and show the larger thumbnail, but we want to write it
01:40in a way that doesn't take away the default functionality when JavaScript is turned off.
01:46So, we are not going to make any changes here in the markup; all we are going
01:49to do is write some jQuery code right up at the top of the document.
01:53What I am going to do is start off by including the jQuery Library.
01:58So I am going to come over here and copy that link that I have in my Index page
02:03and just paste it into my boards.
02:06And remember that the gear folder is one more level down, so I am going to put a
02:10little extra slash in there.
02:12So once I have done that, I am going to write a script, and the type is going
02:20to be text/javascript.
02:23What we want to have happen is when the user hovers the mouse over the view
02:27larger item, the thumbnail image will appear and then it will fade out when
02:31they move their mouse back out again.
02:33Recall from earlier we have a helper function that does just that for us and
02:37it's called the hover function.
02:39We want all this to get setup on the document load.
02:42I am going to write a little document ready function. And remember throughout
02:46the course I have been doing something like this, where I say document.ready.
02:51There's a shortcut version that jQuery gives you where you simply pass the
02:55document.ready function directly to the jQuery object.
02:58So I am just going to go ahead and do that here as a shortcut.
03:04What we want to have happen is we are going to use jQuery to get a reference to
03:09that view larger item and remember that it has an id on it of viewlarger and on
03:18the viewlarger item we are going to define a hover function.
03:23Remember that the hover function takes two function arguments.
03:27The first of which is the function that you wanted to have called when the
03:31mouse enters the element.
03:33The second is the function that you want to have called when the mouse leaves.
03:39In the function for when the hover begins, we are going to use jQuery and
03:45we are going to define an offset variable, which gets the offset of the gear item in the page.
03:54Let's scroll down real quick.
03:56So, the gearItem is right here.
04:00It's this div, and this div is what contains the little thumbnail, the smaller image.
04:05So there's the image and it has the little viewlarger and the other links in it.
04:09So we are going to get the id of this guy right here, because that's
04:12whose position we want.
04:15So we are going to use jQuery to do that and we are going to get the
04:21gearItem and we are going to use our CSS function offset and recall from
04:28our CSS chapter, this gets us the offset information for that item and this
04:33works on cross-browsers.
04:35Now that we have the offset information for the gearItem, we are going to use
04:40jQuery to move the overlay on top of the small image.
04:45So to do that, recall that the tooltip is contained within a div named tooltip1.
04:54We are going to set some CSS properties on that.
04:57We are going to set the top property to the offset.top that just came back.
05:05Then we are going to use a little statement chaining to do the same thing
05:09with the left property.
05:12I am going to do offset.left, and then finally we are going to make it visible,
05:18and we will do that with some CSS.
05:21And we will do display and we will call block.
05:27Now, again, there's a lot of ways to skin this cat.
05:30I am doing separate CSS calls.
05:32There is probably more efficient ways to do this, but for the sake of
05:35illustration, this way it makes it pretty clear to see what's going on.
05:38Then after we have positioned it, made it visible, we are going to animate it up.
05:45So once again, we get tooltip1 and we are going to animate the opacity value up
05:56to 1.0, over the course of 300 milliseconds.
06:03So it's going to happen very quickly.
06:04So that will make it visible, and in fact, let's just quickly test that in the browser.
06:11So, we will bring up the site, go to the gear, go to the boards, mouse
06:16over, okay there it is.
06:17It's not going away however.
06:20So we are going to fix that.
06:22So that's this function here.
06:23So to make it go away when the mouse moves out, we will just write another
06:26jQuery statement that tells tooltip1 to animate its opacity setting down to zero,
06:40over the course of 300 milliseconds and we are going to define a callback
06:45function and the callback function is going to set tooltip1, and we are going to
06:59set its display back to being invisible.
07:05So, that's all there is to that.
07:07We are now at a point where we can go back to the browser and test this out.
07:11So let's switch back to the browser.
07:13Let's refresh and now when we move it up, you can see it's fading in. It's fading out.
07:19Everything is all nice.
07:21So that's creating a tooltip.
07:23Let's move on now to the next feature.
Collapse this transcript
Making an image selector
00:00For the next lesson we're going to use jQuery to build an Image selector.
00:06You've probably seen these before.
00:08You've probably even built one before.
00:10Let me just open up the Groundswell_Final site
00:13so you can see what I'm talking about.
00:15So, let's go to the Lifestyle section and click on Photos.
00:20You can see that as I click on the different photos the image in the large image
00:26area changes whenever I click on one of the thumbnails.
00:30You can also see that this text down here in the caption area, that's changing as well.
00:34Now, you've probably built one of these before and they are relatively
00:37straightforward to build.
00:38But we're going to use jQuery to build one that is inherently extensible and
00:43can be accomplished in just a couple of lines of code.
00:47So, let's go build that now.
00:49Let me go back out to the file system here.
00:54So, let's go to the Start site.
00:57I'm going to open up the site in my editor.
01:04So, this is the home page. What we need to open up actually is the images page.
01:09So, I'm going to go to the Lifestyle, and open the photos page. Okay!
01:13So, this is photos page.
01:14So, just put a design view really quick.
01:16So, here are the photos.
01:18This is the large photo right here.
01:20You can see it has an id of photo_large on it.
01:23Then is this guy here, this is the caption area.
01:26And over here are the individual thumbnails for each one of the images.
01:31So, let's go to the code view.
01:36You can see that each one of the images has an id, and an alt attribute set,
01:42and a class and so on.
01:43So, what we're going to do is encase each one of these images in a link, so that
01:50when they are clicked on, the event will be handled by our jQuery code.
01:56So, what we're going to do is put an anchor or a link tag in front of each one
02:00of these guys, a href=, and then we'll supply a pointer to the image and then
02:10we'll close the link on each one of these guys.
02:14So, the images are located a directory up.
02:18So, I'm going to do that and then, _images.
02:22The way the images are named, you can see that in this case it is pointing
02:27to ventura_01_thumb.
02:29The large version doesn't have this _thumb on the end.
02:33So, we're just going to copy that and scroll back and paste that in there and it's .JPG.
02:44So, now we're going to do that with each one of these guys.
02:50So, I'm going to Copy and I'm going Paste, and Paste, and Paste.
02:54And we'll update each one of these indexes.
02:58Now, we have links that are pointing to the images.
03:03There's a couple of other things that we need to do.
03:04What we're going to do also is place a Title attribute on the link.
03:10The title is what we'll use for the caption.
03:14So, for this title it is "Getting ready to hit Surf."
03:23And this title is "Clearly marked!"
03:30And this title is "Checkout the windsurfers in the background," and
03:43then the last title is "Winding down."
03:50Now, we've got the links wrapped around each tag.
03:54And we've got titles on each.
03:56They'll become our captions.
03:57The idea here is if we wanted to add more thumbnails, we would just be following
04:00this convention and the jQuery code would just pick this up automatically.
04:05So, what we need to do now is write the jQuery that will actually handle these events.
04:12So, up here at the top.
04:13We are into the usual suspects.
04:16We're going to include a link to our jQuery library.
04:20We're going to say Script and the type is equal to JavaScript.
04:32The source, I'm going to point that to jQuery library.
04:35And our jQuery library is located one directory up in scripts, and that is
04:43jQuery-1.3.2.min.js.
04:52Now, we've got the script included.
04:54What we need to do now is write our script that is going to handle the image clicking.
05:01Let's do that.
05:06So, here is what we want to do.
05:07When the document loads, we're going to setup the event handling code to handle
05:11clicks on each of the images, and then set the large image to be the large
05:16version of the thumbnail that was clicked on.
05:18I'm going to write a jQuery function that executes when the page loads.
05:26And what we want to do is loop over all of the link tags that we just added and
05:32setup click handlers on each one of them.
05:36We only want to do this, however, for links that are enclosing images that have
05:41the Gallery class applied to them because the gallery.css style sheet indicates
05:45that it's a thumbnail in the image.
05:46Just go down, and you'll see what I'm talking about.
05:48So, here on this image, you can see each one of these guys has a Gallery class.
05:52So, we only what to do this for links that have the Gallery class applied.
05:55So, what we're going to do is write a little jQuery to figure that out for us.
05:59So, we're going to look for links and we're going to use the jQuery has selector.
06:05Remember this is one of the content selectors I talked about.
06:08So, we're going to look for links that have image with Gallery class applied to them.
06:16And assuming we find any, we're then going to assign click handlers to each one.
06:20The click handler is going to be a function.
06:27Inside the click handler, what we're going to do is declare a couple of variables.
06:32So, first we're going to declare a variable called largePath.
06:35We're going to point that at this.
06:39So, remember, when this function gets called it's getting called because of a click on a link.
06:44So, that this keyword will point to the link object that this applies to.
06:48So, we're going to get the attribute for the href from the link.
06:57Then we're going to get the caption from the title.
07:06So, now we have the caption and path from an href and title.
07:11So, now what we need to do is go find that large photo and that guy should be
07:15right around here somewhere.
07:17So, down here in the mainContent. Here is the image.
07:23That's the photo_large.
07:25So, we need to set that guy.
07:34So, we get a reference to the large photo.
07:36We're going to set its attributes.
07:38So, we're going to set the source attribute to be the largePath that we just defined.
07:48And then, we're going to set the contents of the caption element. Where is that?
07:57Caption element should be down here somewhere. Yup!
07:58There's caption1 right there.
08:00In fact, we can get rid of these other guys from the base site, because we
08:03don't need them anymore.
08:05We're going to set the content of caption1.
08:08We're going to set its text content to be the caption that we just applied.
08:17Now, the last thing that we need to do is we don't want these links to be followed.
08:20We don't want the browser to actually try to follow the link.
08:23So, we need to return False from this event handler.
08:29That will tell the browser, don't worry about it, we've handled it.
08:32You don't need to go ahead and follow the link.
08:34And we've written the code.
08:35Let's make sure everything looks right. Okay!
08:37Let's go back out to the browser.
08:41So, now we're going to open up the version in the Start folder.
08:48So, yup, this is the start version.
08:49Let's go to the Index page, go to the Photos.
08:53So, you can see now that as I'm clicking both the caption and the large
08:58image are changing. All right!
09:02This is extensible.
09:03So, now if I go back to the code.
09:06Let's imagine that we added a whole bunch of new images and we'll just do a Copy and Paste.
09:10So, now if we go back to the browser and we refresh,
09:15you can see that a whole bunch of new images have been added, but it's still
09:19responding to the same event handler.
09:22We'll didn't have to update the event handler code, at all.
09:24That's using jQuery to build an image selector.
09:27Let's move on to the next example.
Collapse this transcript
Using the Resizable effect
00:00This feature that we are going to add to our Groundswell site is the ability to
00:05resize the form element for entering comments on our registration form and this
00:12is going to illustrate using one of the jQuery UI interactions, which is the
00:16Resizable interaction.
00:18So what I am going to do is click on the Register link here and you can see that
00:23there is a Comments field here and it's pretty sizeable.
00:26The user can type their comments.
00:27But what if we want to make this resizable so the user has more area to type in?
00:33So what we are going to do is go over to the code and implement the jQuery UI
00:38Resizable interaction to let the user resize that comments area.
00:43So let's go to the code.
00:47So here we are in the folder for the example, and inside the lifestyle,
00:52there is a register file.
00:54I am going to open that up in my editor.
00:56Let's find that in the design.
00:58So that's down here in the code.
01:02So this is the text area right here, and this text area, we are going to make
01:07resizable and you can see it's wrapped inside of a div named comments section,
01:13and the text area itself has an id of comments.
01:16So a couple of things we are going to do.
01:18First, we need to include the necessary jQuery library and the jQuery UI library
01:26at the top of the file.
01:27So we are going to go back at the top and we are going to do that.
01:30And we also include the CSS file.
01:33So I am just going to copy and paste this and the file is in the sunny
01:41sub-folder because that's the thing we downloaded.
01:43And it's called jquery-ui-1.7.2.custom.
01:55Okay, so now we have included the style sheet. Now we need to include the script file
02:00for both jQuery and jQuery UI.
02:03So we are going to do that.
02:05We are going to type script and the type is equal to JavaScript and the source
02:14is the scripts folder and it's jquery-1. 3.2.min.js and that's that script right
02:26there and then we are going to include the jQuery UI and that's
02:32jquery-ui-1.7.2.custom.min.js.
02:40All right, so now that we have included the script, we can assign the resizable
02:45behavior to our text area.
02:49So let's go ahead and do that and remember that's just a simple matter in
02:54jQuery of when the page loads, and I am going to declare my little page load
02:59function right here.
03:02So when the page loads, we are going to use jQuery to get a reference to the
03:10Comments section div and we are going to make that resizable.
03:17So that's pretty much all we need to do to make it resizable.
03:20Let's see how that works out.
03:23So jump over to the browser.
03:25I am going to refresh.
03:28So you can see now that a little resize handle has been added here at the bottom
03:33of the Comments field, so we can resize.
03:37But you can see that there is a bit of a problem.
03:39We don't want the user to be able to resize this thing across the entire page.
03:43It will be nice if we can just constrain it somehow so that they can't make it
03:47too small or too big.
03:49So let's set about solving that problem.
03:52So let's go back to the code.
03:55So what we need to do is specify that there is a maximum and minimum width for the field.
04:04So here in my function, I am going to declare a couple of variables.
04:08First, I am going to declare a maxWidth variable and what I am going to do is
04:14I am going to set that to be whatever the current width of the field is.
04:18That way it can't get any bigger than it currently is.
04:20So I am going to use jQuery to retrieve a reference to the Comments section div,
04:27and I am just going to ask for its width.
04:32Remember from our CSS section that we can get the width of any element by doing this.
04:37Now in the resizable call, pass in some parameters to resizable, because
04:42remember it takes some parameters like what we want the min width and max width
04:45to be, and so on and so forth.
04:47So we are going to pass in an object and I am going to say that the maxWidth
04:52can't be any more than the maxw.
04:55That's should fix the max problem.
04:57Let's go back to the browser and see.
04:58So I am going to refresh and yeah, sure enough, it can't get any wider than it used to be.
05:07That's its maximum, so that's good.
05:07I still have to fix the minimum problems though.
05:10So let's go back to the code.
05:13So we are going to do the same thing for the minimum width and minimum height.
05:16I am going to declare another variable named min width and I am going to set
05:22that to be equal to max width.
05:23So that way, the text area will only stay as wide as it currently is.
05:30It won't get any more narrow and it won't get anymore wide.
05:33And finally, I am going to declare another variable and this one is going to
05:35be called min height.
05:38I am going to do the same kind of trick that I did with the width.
05:40I am going to copy that, I am going to paste this and now I am going to ask for
05:44whatever the current height is.
05:45Okay, so now, I know how wide it is and how high it is.
05:48I did just set the right parameters. There is maxWidth.
05:53I am going to do min height and that's minh and I am going to do min width and
06:04that will be minWidth.
06:07Now I have set all the right parameters, all I need to do now is save, and
06:11we will go back to the browser, and now we will refresh.
06:15So now you can see that I can only resize the element up and down.
06:20I can't make it any wider, I can't make it anymore narrow, and I can only resize
06:24it down to the point where it was as high as it was to begin with.
06:29So I have pretty much used jQuery UI now to provide a feature to my users.
06:33They can now type in the field and now they can enter text and if they don't
06:38think it's big enough, well, they can just go ahead and resize that down.
06:41I will just scroll the page down.
06:44So they can make it as nice and large as they want to.
06:46And just to make sure it works cross browser, let's try it on Firefox.
06:54So I am going to open this up in the Firefox browser and I'll scroll down and
07:01you can see, yup, there is the resize handle, and sure enough, yup, working just the same way.
07:07So that's pretty much all the features that we are going to use from the jQuery UI library.
07:11However I highly encourage you to experiment.
07:14You can take the source code for the Groundswell site as you have it in your
07:18example files folder, if you have access to the example files.
07:21Feel free to play around with the jQuery UI settings and see what you can make happen.
Collapse this transcript
8. Accessing Data with AJAX
Working with Asynchronous JavaScript and XML (AJAX)
00:00It's time to talk about AJAX.
00:02AJAX is used all over the web these days.
00:05Using AJAX, your web pages can retrieve data from remote services and communicate
00:10with your web server without having to reload the entire page.
00:15Over the past several years AJAX has become one of the foundations of building
00:20modern web applications.
00:22jQuery makes working with AJAX really, really easy.
00:26If you're not familiar with AJAX, I highly suggest that you go take a look at
00:31Ray Villalobos's JavaScript and AJAX title, which is right here on lynda.com.
00:36I have got it up right here in the browser so you can see it.
00:40I am not going to go deeply into AJAX and what it is and how it works, because
00:44Ray has already done that, and I highly suggest you watch this title if you need
00:49to get familiar with AJAX.
00:50The other thing I want to point out is that I'm going to be using a different
00:52tool in this chapter.
00:54I am going to be using a tool called Aptana Studio, and that's this tool right here.
00:59And the reason I'm going to be using Aptana Studio is because it has a built-in
01:03local web server that I can use for testing.
01:07So I don't have to upload my pages to my web server in order to test out
01:11the AJAX functionality;
01:13I can just do it right here on my local machine using the local web server that
01:17Aptana Studio has built-in.
01:19You don't have to use this if you want to.
01:22You can follow along with me, and you can post your files up to your web
01:26server, or use a local web server that you may already have configured on your local computer.
01:31That's up to you.
01:32I'm going to be using Aptana because I happen to like the tool, and it has a
01:35built-in local web server.
01:37And if you want to use it, I also recorded a title here on lynda.com, Up and
01:41Running with Aptana Studio 3.
01:43If you want to learn how to use Aptana Studio, it's a completely free product.
01:48It's a really great editor.
01:49I've covered how to use it in this title here.
01:52It's only about an hour and half long.
01:54Let's go back to Aptana Studio.
01:56So here in Aptana Studio I have imported my exercise files as a project here, and
02:02I'm going to open up the basic_start file.
02:07And what we are going to show is how to use the AJAX function.
02:12Now, jQuery provides a whole bunch of AJAX-related functions. In fact let's go
02:17back out to the browser, and let's head over to api.jquery.com, and we will scroll
02:27down to the AJAX section.
02:29So AJAX has a low-level interface that we are going to be looking at called the
02:36AJAX function. That's this right here.
02:37And the AJAX function performs an asynchronous HTTP request to the server.
02:44And there is a whole bunch of settings and properties that you can pass to the function.
02:50I am going to scroll down to some of these.
02:52Again, I'm not to go over all of these because some of these are pretty advanced,
02:55and some of them you'll probably never touch in your development. But you can
02:59see that there's quite a few of them.
03:01But the way that it works is you simply use the $.ajax function to simply make
03:06a request to a server and get a response back, and that's what we are going to do in our example.
03:12So let's jump back to the code.
03:14Now, there's a couple things I have that you should be aware of.
03:17I have got two different helper sample data files here.
03:21This one is testdata.txt.
03:25This one is testxmldata.xml. And we'll be using both of these in this chapter
03:31as the files that we'll be retrieving from our server, in this case a local test server.
03:37Okay, so let's close that for now. Let's go back to basic_start.
03:42Here we are in the page, and you can see that in my document's ready function I
03:47have a function here called getData. getData is the function that we are going
03:52to fill out that's going to retrieve data from the server.
03:56Here in the getData function what I am going to do is type $.ajax, and then
04:04inside this function I'm going to pass a whole bunch of properties, so let's
04:10go ahead and do that.
04:10The first thing I'm going to pass in is a property called the URL.
04:14This is the URL of the data that I want to get information from.
04:21In this case, I am just going to use the testdata.txt file, and you can see it's
04:27at the same level in the folder as this HTML page is, so I don't have to write
04:33any path. I am just going to say testdata.txt.
04:37So that's the first one.
04:39The next property I am going to put in is what kind of request this is, and
04:44that's the type property.
04:46It can either be get or it can be post, depending on what kind of HTTP request
04:52you're going to use.
04:53Get requests are usually used for operations that are not destructive; in other
04:58words they don't change data on the server.
05:01If I was going to be changing data on the server, instead of using get I would use post.
05:07But that's not what I'm doing.
05:08I am simply retrieving data, so I am going to use get and leave that the way that it is.
05:13Then I'm going to specify what kind of data I expect to get back, and that is dataType.
05:19Now, dataType could be a bunch of different things. It can be text.
05:22It can be a ray buffer.
05:24It can be a whole bunch of things to represent binary data.
05:27I am just going to say text because I'm expecting to get text data back, and you
05:31can see here that that's exactly what this is.
05:33It's just textual data.
05:35Now I've specified the major pieces that I need to specify.
05:38The only thing to do now is tell jQuery what to do when things go right and
05:44when things go wrong.
05:45I am going to say on success you should call the successFn, which is this
05:56function right here.
05:57And then if something goes wrong, error, you should call the errorFn, which is
06:07this function right here.
06:10The next thing I am going to do is say, you know what, no matter what happens, whether
06:14things went right or whether things went wrong, there is another function you
06:18should call, and that's called complete, and on complete I am going to supply a
06:25function just directly in line here, and it's going to take the XML HTTP request
06:29object along with a status indicator.
06:31And this function is just going to say console.log.
06:39The request is complete, and we will close that off.
06:45All right, we are requesting the text data.
06:48We are using a get request.
06:50We are expecting to get text back, and we've got functions for when things go
06:53right, functions for when things go wrong, and a function that just gets called regardless.
06:59All right, now in the success function we need to say what to do with the
07:04data that comes back.
07:05So when this function gets called, result is going to be the data that came
07:10back from the server.
07:11What I am going to in that case is, if you look down here in my document, I have
07:16got a paragraph with an ID of content.
07:18So what I am going to do is use jQuery to get a reference to that content
07:22paragraph, and I am going to use the append function to append the result data
07:29into that paragraph, so now I am going to save it.
07:33What I am going to do now is I'm going to run this in my local web server, and in
07:39Aptana Studio I can do that up here my Run menu. I can run it using a variety of
07:44different browsers that I have using the local server.
07:49If this looks different for you, that's okay.
07:51You can always look at the run configurations here, and down here in the
07:56web browser section you can add your own local server with whatever
08:01browser you happen to be using.
08:03The instructions are right here, so let me close this.
08:07What I am going to do is run this using Chrome as my local server.
08:11So let's go ahead and do that, and you can see that here in the document, this is
08:16the paragraph with this gray outline around it.
08:18You can see that this is some text data.
08:21This is the contents of that test data file that was on the server. Ao the
08:25AJAX function went out and requested the data, got it, came back, and put it in this paragraph.
08:30Just to show you that that's what's happening, let's bring up the
08:34Developer Tools in Chrome.
08:36Here is my Developer Tools.
08:39And what I'm going to is I am just going to crank up the font size a little bit
08:41so you can see what's going on in here, and I'll just make this a little bit
08:46wider, alright, okay.
08:49And over here in the sources what I am going to do is put a breakpoint right here.
08:58So I will put a breakpoint right there.
09:01Let's refresh it and see what happens.
09:04Okay, so we are going to refresh, and you can see that the breakpoint has now
09:10been hit where the AJAX function is about to get called.
09:14Okay, so there is the AJAX object.
09:16It's going to be looking for the testdata.txt, okay.
09:20So now we have breakpoint down here in the success function, and we'll see what
09:24result is when the function gets called and comes back.
09:28All right, so I am going to go ahead and click Run, and you can see that now we
09:31have hit the breakpoint here in the success function, and if I scroll over and
09:36hover over this result, you can see that that's the same content of that
09:40test data file that we have on the server.
09:43So the server came back, and we have a result of okay, and we're just going to go
09:47ahead and append that result into the paragraph, so let's step right.
09:52And I am going to step over this function.
09:54Now, watch up here in the paragraph, when we step over and you can see that that
09:58data got inserted in.
09:59That's essentially how you use the base AJAX function in jQuery to retrieve
10:06data from a server.
Collapse this transcript
Using AJAX helpers
00:00In addition to the basic AJAX function, jQuery provides a set of shorthand helper
00:06methods that help cover some common scenarios that arise when using AJAX with
00:11jQuery, and we're going to take a look at a couple of those now.
00:14So here in the jQuery API documentation, I'm going to click in the AJAX section
00:19on the Shorthand Methods.
00:21You can see there is a small set of these.
00:23The get function loads data from the server using an HTTP get request, and that's
00:29sort of like using the AJAX function with the type property of get, only this is
00:34a shorter more convenient way of doing that.
00:37The get function has a couple of specialized counterparts.
00:39There is getJSON for getting JSON data from the server, and then there is the
00:44getScript version, which loads a JavaScript file and then executes it.
00:47There is the post function, which is again, kind of like using the basic AJAX
00:51function, with a post option, but this is a more shorthand way of doing it.
00:55Then there is the load function, which loads data from the server and takes the
00:59content that comes back and puts it into the matched element.
01:02And so we are going to take a look at using the get function and the load
01:06function in this movie.
01:07So let's take a look at the get function.
01:10The get function takes a URL and it takes any data that you want to send to the
01:14server--so these could be you know URL- encoded properties that would normally be
01:18sent on the query string.
01:20And then there's a function to call and case things are successful, and then there
01:24is an optional dataType.
01:25Now if you don't provide a dataType, the jQuery library will just simply try to
01:30make an intelligent guess as to what kind of data you're looking to come back,
01:34either XML JSON, text, whatever.
01:37This is a pretty simple function to use.
01:39Let's go back over to our code and take a look at an example of using it.
01:43So here in Aptana Studio, I've got my Helpers_start document open, and it
01:50looks pretty similar to the one that we used in the previous movie for the AJAX function.
01:55Here, on my document ready function, I have a function called getData, and getData
02:00is what we're going to fill out.
02:01Now, last time when we did this-- remember we did $.ajax and that's not we're
02:06going to be doing this time.
02:07This time we are going to be using the get function.
02:10And to do that I am simply going to write $.get, or of course I could have
02:15written you know jQuery.get, but dollar sign is more convenient.
02:18So I write $.get, and I am going to get the same text file, so it's going to be
02:25testdata.txt. And then I need to give the name of the success function
02:29and that's this function right here, successFn.
02:35When successFn is called, the result will be the data that came back from the
02:39server, and then we're going to get a reference to our content paragraph, which is
02:43right here and put the result inside of it.
02:47Okay, so we're going to save this, and then once again, using Aptana Studio, I am
02:52going to run this using my local web server. And you can do this however you want to.
02:56You can post this to whatever server you use it for testing or whatever IDE
03:01you happen to be using.
03:02But I am going to run this using my local server here in Aptana, so I'll bring it
03:06up in Chrome, and you can see that what's happening is the result is coming back
03:10from the server and being placed into the paragraph.
03:12All right, so let's go back to the code.
03:14Now, it turns out that this combination right here of getting data and then using
03:19the successFn to put the result into an element on the page is so common that
03:25jQuery actually gives you a shorthand way just of doing that, and so let's go
03:30back up here to get data and comment this out.
03:32What we're going to do this time is simply, on the element itself, dollar sign
03:37and we're going to get the content paragraph,
03:40we're going to call the load function.
03:42And the load function will actually go out and kick off an AJAX request, and
03:46we're going to ask for testdata.txt.
03:50Now, that's all that we need to do.
03:52So this right here, load, is going to go out to the server, look for this URL,
03:59take the result, and put it into whatever element matches this selector right here.
04:04So we are going to save, and just to be sure that we're not somehow
04:08triggering the successFn,
04:09let's go ahead and comment that out.
04:11So we'll save and we'll run this again using the internal server, and you can
04:16see that it worked.
04:17So we refresh, and you can see that the text data is coming back from the server
04:20and being put into the element on the page all with one line of code.
04:24So if you have a need to use AJAX that fits one of these comment scenarios then
04:29the jQuery AJAX helper functions can make your job a lot easier.
Collapse this transcript
Understanding AJAX data types
00:00In this example we're going to see how to use jQuery's AJAX features to deal
00:04with different kinds of data coming back from the server.
00:07Because data won't always be coming back in the form of HTML or text, sometimes
00:12it will be JSON, sometimes it will be XML, sometimes it will be binary format,
00:16jQuery's AJAX functions make dealing with different kinds of data pretty easy.
00:22Back here in the API documentation for jQuery, I am going to click on the
00:26Shorthand Methods again.
00:28Let's take a closer look at the jQuery.getJSON function.
00:33getJSON is basically shorthand, which is equivalent to just calling the
00:39AJAX function that you can see here, with a dataType of json, a url, a data
00:44and a success function.
00:46And so here the arguments are,
00:47there is a url, and that's the url that you want to get the JSON data from.
00:52There is the data object, which is optional.
00:54It's the properties or data, or you can think of parameters that are sent to the
00:59server with the request, and then there is a success function, which is called
01:03when the function returns successfully.
01:06Let's take a look at how we can use this in practice, so let's jump over to the code.
01:11Here in the code I have my DataTypes_start document open, and what we're going to
01:17do is first work with some JSON data.
01:21We're going to write a function called getJSON data when the document loads, and
01:28that's this function right here.
01:30Now, I have already put in a definition for the Flickr API.
01:34So, Flickr is an online photo service which provides a whole bunch of pictures,
01:39and it has a freely available JSON service where we can call the API provided by
01:45Flickr and get back photos that match a certain kind of tag.
01:49Now, I am not going to go deep into how Flickr works.
01:51You can look it up on your own time.
01:53But we're going to use Flickr in this example because it has an option for
01:55providing JSON data.
01:57So what I am going to do is on the next line here, I am going to write $.getJSON,
02:04because we're going to get some JSON data. And the URL that we are going to use
02:08is this Flickr API URL right here.
02:11So I am going to pass that in as the first argument, so that's Flickr API, and
02:15then I need to provide some parameters to the Flickr API so it knows what
02:20photos I'm looking for.
02:22I am going to pass in, first, I am going to ask for tags, which will be, let's see.
02:28Let's look for photos having to do with the space needle, and tag mode will be any.
02:36And again, these parameters are specific to Flickr.
02:38You can look up what these mean.
02:40And the format is going to be json, so we are telling Flickr that we want data
02:46back in JSON format.
02:48Let's assume that that works. What we are going to do then is call the successFn,
02:54and the successFn is down here.
02:59When the successFn gets called, this result right here is not going to be a
03:03text string or HTML.
03:05It's going to be JSON, and it's going to be a JSON object.
03:09And so what I am going to do is use jQuery's each iterator to iterate over each
03:15one of the items that came back in the result set.
03:18Now again, this is going to be determined by what the structure of the JSON data
03:23that you get back is.
03:24Now, I happen to know that Flickr comes back with an items structure inside the result.
03:32So for each thing in the result.items-- and I am going to declare a function
03:37that's going to handle each one of these and it's going to take an index and
03:41it's going to take the item itself.
03:43So for each one of these guys, what I am going to do is make a new image tag,
03:49and I am going to do that by using jQuery's HTML constructor to make a new image tag.
03:55And the image tag, I am going to give the attribute for the source.
04:00So I am going to set the source attribute for the image to item.media.m and
04:07again, this is specific to Flickr.
04:09So, once I've set the attribute, I am going to take that image and I am going to
04:14append it to the content div.
04:19Once again just a review, make a new image, set the source attribute to the
04:23source inside the JSON structure for that particular item, and then append
04:27the whole thing to the content div, which is this guy right here, with the id of content.
04:33So, now I need to limit the number of photos I am getting back, because his
04:37is going to come back with a whole bunch of photos, and I don't want to do a whole bunch.
04:40What I am going to do is say, if i == 4 then we're going to just return false,
04:48which tells this each iterator right here to stop running, and that should limit
04:52our results to just 4 photos.
04:55So, it looks like, if I've done this correctly, I am going to save this and now I
04:59am going to run it in the browser, okay.
05:02So let's go ahead and run this in my local server, and you can see that the API
05:09went out to Flickr and got a whole bunch of photos tagged with the space needle.
05:13Now, these pictures are going to look different for you based upon when you run
05:18this example, so here you can see this is a pretty clear Space Needle photo.
05:22Here is one where it's way back in the background. The others are just Seattle.
05:26I am not sure why they came up, but they were tagged with Space Needle.
05:30So let's bring up the Developer Tools so that you can see what's happening.
05:33So here in the successFn, what I am going to do is put a breakpoint, and now I am
05:40going to refresh the page, and you can see there is the breakpoint being hit,
05:44all right, and here is the result.
05:45Now you can see that the result is a JSON structure.
05:50This JSON structure is defined by Flickr.
05:52But you can see that there is an array of 20 things here in the items array, and
05:57each one of these is an object, right, and each object has a whole bunch of
06:01information about the picture and here is the media property, and there is the
06:06URL to the photo itself.
06:08So, what I am doing is I'm iterating over each one of these results.items set,
06:12and then for each item I'm building up the individual image.
06:17So I'll take the breakpoint off and let it go, and we'll close the Developer Tools.
06:21Okay, so that's dealing with JSON data.
06:23Let's go back and take a look at how to deal with XML data, because XML data is
06:27another common piece of data that you work with on the Internet.
06:30So let's scroll back up. Then we'll comment out that function and call another
06:34one called getXMLData.
06:35And getXMLData is this function right here, so let's fill that one out.
06:43And for this example we're going to use our test XML data.XML file, and I just
06:50made this up. I threw some XML in here there is my name.
06:54Here is the title, so this is what we're going to use.
06:57Back here on the code, let's write the code to get the XML data.
07:00In this case I am going to use $.get,
07:02and just get, I am not getting JSON here, so all I am going to do is ask for XML
07:07data, and I need to get the test
07:09xmldata.xml file. And this time I am going to write my own result function
07:16just right here inline.
07:18So I am going to write function result, and now we are going to flesh this out.
07:24Now in this particular case when this function gets called, result is going
07:28to be an XML document.
07:30It's not going to be XML string data.
07:32It's not going to be text.
07:34It's going to be a parsed XML document, and that means I can use DOM XML
07:39functions on that result.
07:41For example, I can do things like var title = result. Because this is a document, I
07:47can say getElementsByTagName, and I will get to title first, and that comes back
07:55with an array, so we get the first one.
07:57Okay, and then I'll do the same thing for var name =. And I'll just use this
08:02again, copy and paste, only this time I want the name.
08:08That gives me the tags for title and name, and now I am going to say var val =
08:15and I am going to say title.firstChild. nodValue. And again this is regular old XML, okay?
08:26In the title, the first child of this title tag is the text inside the tag.
08:31That's just how the DOM works.
08:32So I am going to say + " by " + name.firstChild.nodeValue. So now I've got the
08:48title by name, and then all I need to do at that point is say $ and get a
08:56reference to my content div and say append that value.
09:02Now, we're calling function when the document loads, so let's save this and
09:07watch what happens.
09:08So I am going to run this in my local server, and you can see it says jQuery
09:11Essential Training by Joe Marini, alright.
09:14Again, let's bring up Dev Tools so you can see what's happening.
09:18I am going to put a breakpoint right here inside the result function, and then I
09:23am going to refresh.
09:24And you can see that there is the breakpoint, okay.
09:26So, here is the result, right, and you can see that the result is a document.
09:30It's an XML document.
09:31So, jQuery correctly identified the incoming data as XML, parsed it into an XML
09:36document, which I can now use my XML DOM functions on.
09:40So for example if I step over these functions, you'll see that title, right, is a node.
09:46That's a title tag, and name is a tag, right.
09:50Then when I step over this function, you can see that I now have a string that
09:54extracts the value of each one of the contents of those two tags, and then here
09:59is the content where I do the appending of the value into this div.
10:03So watch this area right here. So when I step, you can see that the string got put in there.
10:08In a nutshell, that's how you work with different kinds of data types using the
10:12jQuery AJAX functions.
Collapse this transcript
Using global AJAX event handlers
00:00The jQuery AJAX library provides a set of global AJAX event handlers that you
00:06can use to register functions to listen in for interesting events that take
00:10place during the lifecycle of an AJAX request on a given page.
00:14Here in the API documentation let's go ahead and click on the global AJAX event
00:18handlers, and you can see that there is about a half dozen functions that you
00:21can use to register events for when something interesting happens in an AJAX request.
00:28Now again, these are not functions that you call; these are functions that
00:32get called for you.
00:33And so for example, you use the AJAX send function to register an event handler
00:39that gets called when an AJAX request is about to be sent.
00:44And again, these are global, so these are for all of the AJAX requests on your page.
00:49So for example, you might use the ajaxStart and ajaxComplete events to show some
00:55kind of user interface on your webpage to let the user know that there is an
01:00AJAX request taking place, and that they need to be patient while data is
01:04retrieved from the server.
01:06You might use, for example, the ajaxError event handler to centralize all of
01:11your error handling rather than attach individual error functions to each one of your requests.
01:17Let's take a look at these events in action.
01:20We're going to jump over to the code here, and here in my code I've got
01:24Globals_Finished up in my editor, and that's this file right here.
01:29So you can see that I've got a whole bunch of functions. There is Start.
01:33There is Stop. There is Send, Complete, Error, and Success.
01:38So I've registered functions, and basically what I am doing in each one of these
01:42functions is just using the developer console to log some message about
01:46something happening with AJAX.
01:48And then after all of these global event handlers have their functions
01:52registered, I call this get data function right here. And getData, all it's doing
01:57is using the get shorthand method to get the testdata.txt file, which we've been
02:02using in this chapter, and there is a successFn right here. And in the successFn
02:07we just simply log out that the result is being set, and we put the contents of
02:12the result parameter right here, into this content paragraph right here.
02:18So let's save that.
02:20Let's bring this up in the browser, and let's see what happens.
02:24So I am going to run this in my local server.
02:28So you can see that "This is some text data."
02:30got set into that paragraph.
02:32Let's bring up the Developer Tools, and let's switch over to the Console, and you
02:37can see that in the order in which things happened, each one of those event
02:41handlers put out a little string to the Developer Console.
02:46So right here we have AJAX is starting and then it says About to request data and
02:50then Setting the result and then it says Looks like everything worked!
02:53Everything's finished! The AJAX request ended.
02:56And if we go back to the code--let's scroll up--
03:02you can see that those strings correspond to each one of these functions.
03:08So for ajaxStart it says AJAX starting, and then ajaxStop it says AJAX request
03:12ended and so on and so forth.
03:15Using these global event handlers, you can register events that will get called
03:19for every single AJAX request.
03:21In fact, let's take a look at what happens when an error occurs, and we have our
03:26ajaxError handler right here. And this function takes some parameters. So do the
03:31other ones as well, but they're optional, and I'm not using them in this example.
03:35But here in the case of ajaxError, we have parameters for the event, the jqXHR
03:40object, any of the AJAX settings, and the actual error that happened.
03:44So let's scroll down, and make a request for a file that we know
03:49doesn't exist, right.
03:50So testdata1.txt is not real, so let's save that and then let's go back to the
03:54browser, and now let's refresh the page, and you can see that it didn't work.
03:59When we bring up the Developer Tools, right here we have our AJAX starting.
04:03It says About to request data, but then you could see that there was an error.
04:07It seems like requesting testdata1.txt was a problem.
04:10If we expand that out, we can see that there was a whole bunch of things that
04:14took place in here, right, and then it says, Hmmm.
04:17seems that there was a problem and then not found.
04:20But you can still see that Everything's finished!
04:21and AJAX request ended functions still completed.
04:25So let's go back to the code.
04:28In this case, rather than the ajaxSuccess function being called that says looks
04:34like everything worked,
04:36this time the AJAX error function got called and it says Hmm.
04:39Seems Like there was a problem, and the not found message is passed in by jQuery
04:45as the error of why things went wrong.
04:48These global event handlers in many ways provide some nice centralization of
04:52AJAX functionality so that you can provide some really great user experience
04:56to your users like global settings of showing when requests are in progress, or
05:00centralized error handling, and it really makes working with AJAX and jQuery a
05:05lot easier.
Collapse this transcript
Conclusion
Goodbye
00:00Okay, well that brings us to the end of jQuery Essential Training. I hope you
00:04have enjoyed learning about jQuery along with me.
00:07You should now know everything you need to get started using jQuery to build
00:11your own highly interactive and modern web pages.
00:15I hope to see you soon out on the web.
00:17Take what you've learned, and go out there, build great sites.
Collapse this transcript


Suggested courses to watch next:

jQuery Mobile Web Applications (3h 13m)
Ray Villalobos

Creating a Responsive Web Design (1h 31m)
Chris Converse


Responsive Design Fundamentals (2h 15m)
James Williamson


Are you sure you want to delete this bookmark?

cancel

Bookmark this Tutorial

Name

Description

{0} characters left

Tags

Separate tags with a space. Use quotes around multi-word tags. Suggested Tags:
loading
cancel

bookmark this course

{0} characters left Separate tags with a space. Use quotes around multi-word tags. Suggested Tags:
loading

Error:

go to playlists »

Create new playlist

name:
description:
save cancel

You must be a lynda.com member to watch this video.

Every course in the lynda.com library contains free videos that let you assess the quality of our tutorials before you subscribe—just click on the blue links to watch them. Become a member to access all 104,069 instructional videos.

get started learn more

If you are already an active lynda.com member, please log in to access the lynda.com library.

Get access to all lynda.com videos

You are currently signed into your admin account, which doesn't let you view lynda.com videos. For full access to the lynda.com library, log in through iplogin.lynda.com, or sign in through your organization's portal. You may also request a user account by calling 1 1 (888) 335-9632 or emailing us at cs@lynda.com.

Get access to all lynda.com videos

You are currently signed into your admin account, which doesn't let you view lynda.com videos. For full access to the lynda.com library, log in through iplogin.lynda.com, or sign in through your organization's portal. You may also request a user account by calling 1 1 (888) 335-9632 or emailing us at cs@lynda.com.

Access to lynda.com videos

Your organization has a limited access membership to the lynda.com library that allows access to only a specific, limited selection of courses.

You don't have access to this video.

You're logged in as an account administrator, but your membership is not active.

Contact a Training Solutions Advisor at 1 (888) 335-9632.

How to access this video.

If this course is one of your five classes, then your class currently isn't in session.

If you want to watch this video and it is not part of your class, upgrade your membership for unlimited access to the full library of 2,024 courses anytime, anywhere.

learn more upgrade

You can always watch the free content included in every course.

Questions? Call Customer Service at 1 1 (888) 335-9632 or email cs@lynda.com.

You don't have access to this video.

You're logged in as an account administrator, but your membership is no longer active. You can still access reports and account information.

To reactivate your account, contact a Training Solutions Advisor at 1 1 (888) 335-9632.

Need help accessing this video?

You can't access this video from your master administrator account.

Call Customer Service at 1 1 (888) 335-9632 or email cs@lynda.com for help accessing this video.

preview image of new course page

Try our new course pages

Explore our redesigned course pages, and tell us about your experience.

If you want to switch back to the old view, change your site preferences from the my account menu.

Try the new pages No, thanks

site feedback

Thanks for signing up.

We’ll send you a confirmation email shortly.


By signing up, you’ll receive about four emails per month, including

We’ll only use your email address to send you these mailings.

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

By signing up, you’ll receive about four emails per month, including

We’ll only use your email address to send you these mailings.

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked