So now, we are going to take what we have learned in jQuery so far using selectors and filters and apply our knowledge to a pretty neat practical example. So, if you go into the folder for the example files, you will see a couple of files here underneath. AutoPDFIcons and there is a start and a finished and you can go ahead and jump to the finished one if you want to, or you can follow along with me here in the start version. Let me open this up in my text editor and let me show you this file in the Design view. So, you can see here that I have got a file and inside the file is a whole bunch of links.
There are different kinds of links. So, if we go to the Split view, you can see what I mean. So, here is this link and it goes to an HTML file, this one here happens to be a named anchor, this one goes to another HTML, this is one goes to PDF, a couple of more HTML files, another PDF file, this one goes to a mailto link. The thing is though, if you look at the design, you can't really tell that visually. You really can't tell that Link #3 is a PDF file versus an HTML file. So, we are going to write a one line script in jQuery that automatically decorates all the links in a page with little icons that indicate that they go to PDF files rather than HTML pages.
So, what I am going to do is go into the Source view and by now you should be familiar with, including the jQuery library and here is our function that gets executed when the document loads. See, what I am going to do is I am going to write a little jQuery that says get all of the links that have attributes named href and if the href attribute ends with a .pdf string, then we want to include a little icon. So, remember ends with, for attribute filters, is the dollar sign equals and we want it to end with .pdf.
So, that query should get me all the links that end with .pdf and I am going to write a little function now. We haven't learned this in jQuery yet, we will see this in the next section. This has to do with manipulating content and creating some new content in the fly. So, what we need to do is insert an image that references the icon and remember that this query will come back with a set of objects, of all the links that match this criteria. So, for each one of those guys, I am going to say, hey! Insert into the document after each one of those instances.
So insert an image and if you look in the example files folder, you will see that there is a folder names images. So, I am going to point at the images folder and in there you will see a little file named small_pdf_icon.gif and to make it look better, we will just align it with the absolute bottom and then we will close off the image tag. Okay, so this is going to loop through all the links and it's going to find all the links that have an href that end with PDF.
So, it's going to skip over the HTML. It's going to skip over the named anchor, going to skip over the mailto and it's just going to find these guys right here. So, we are going to save that and we are going to bring it up in the browser. So, let's go out to the folder and we are going to just double-click on this folder and bring it up and you can see that when the page loads, that function gets executed and now those icons have appeared after the links that end with PDF. And we can get pretty creative with this.
We can put the links before, we can put it after. We can do whatever we want, but this is a nice practical example of using jQuery to do something really simple in the page that increases the usability. And if you go back to the code, you will see that if I add more PDF links, I will just copy and paste them in and then save it, you will notice that this behavior just automatically appears on all of the links that get added. So now, anytime, anyone adds a new link to the page that ends with PDF, it will pick up this behavior. So, let's go back to the browser and let's refresh and you can see that as those links got added, so did my little icons.
So, it's early on in the course, but already we are building some practical solutions to some very real problems using jQuery and we are going to keep right on doing that to the rest of the course.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 98207 Viewers
61 Video lessons · 85506 Viewers
71 Video lessons · 69469 Viewers
56 Video lessons · 101807 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.
Your file was successfully uploaded.