Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In this movie we're going to look at chaining, which is a way of writing your Query. That's something you'll see all the time in other people's code and something that you will, no doubt, use a lot in your own as well. So, we're looking at this page from the Hansel and Petal website. This the build a bouquet page. If I reload it you can see nothing is happening when I first load the page. I'm looking at the file over here. This is the very bottom of the file where the script tags are, and I have some code here commented out, so it doesn't actually do anything. I have my jQuery ready, but it doesn't do anything, because all of this is commented out.
So let's remove those comments. This code will actually do something when the page loads. I'm going to save this, come back over here and reload. You can see the jQuery is doing some stuff to these elements. So let's look back at the code and kind of take it apart and see what's happening here. And then we'll talk about how to rewrite this using chaining so it's not so verbose. So, what I'm doing here is grabbing, using an ID selector, the grid. Which is a container for all of these list elements. And I'm using Find to grab all of the list elements from inside that grid. And then I'm doing some operations to it.
First time setting the background color, using CSS. I'm animating the width over two seconds, making it shrink by 100 pixels. Fading it out, and then doing a slow fade in. Let's watch that one more time with that in mind. So everything gets smaller, fade out and fade in. Okay, now we're doing this on separate lines saying the same thing over and over again. It's a little repetitive, not that great. So jQuery chaining allows us to rewrite this in a way that's a little nicer. A little more succinct. So, what we do, is instead of using this same variable over and over again we can just attach each action in sequence, separating them with the dots, like this.
So I'm going to save again, come back over here and reload. As you can see it still works. So what this all means, is that each of these is a method In the jQuery object. And so every time I do one of these actions, jQuery returns the resulting object with those changes applied. So, first I do the Find, and then I can take that and apply the css to it. And then I can take that and apply the animation to it. And then I can take that and fade it out, and fade it back in. So this is all on one line now which is more succinct but it's not that all readable. So here's another thing you can do. To make this easier to read, you can break these across separate lines, like this, you're ending it with a semicolon and so you start out with your grid, that you selected up here, and taking each of these actions in sequence.
No matter how you write it, you're still using chaining either way. And as I say, this is something you'll see all the time in other people's jQuery, if you're learning from other people's examples, and of course you'll be writing it this way a lot yourself in your own code.
Get unlimited access to all courses for just $25/month.Become a member
58 Video lessons · 66353 Viewers
61 Video lessons · 96828 Viewers
56 Video lessons · 110524 Viewers
82 Video lessons · 105174 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.