Register | Forgot Login

Selecting and Styling External Links, PDFs, PPTs, and other links by file extension using jQuery

01.29.2009

In this tutorial I will explain how I used jQuery to select and style PDFs, PPT, images, and external links all differently using jQuery and CSS. Here is what an example looks like:

Screenshot showing the example links selected and styled with PDF, PPT, and external link icons

Selecting Links Based on File Extensions With jQuery

To select something based off of the href value of the link we are going to use the jQuery attribute filter selector that checks to see what the characters the attribute value ends with. To select a pdf with this selector would look like this:

$("a[href$='pdf']").addClass("pdf");

Now any link that has a link to a pdf document gets the following CSS applied:

.pdf { background: url(/images/icons/pdf.png) no-repeat 0; padding-left: 20px; |

The problem here is that we do not want to add the class to the link, we want to add it to the list item.  Let's look at the HTML that we are working with.

<ul class="resources">
<li><a href="Week-One-HTML-and-Beyond.pdf">Week-One-HTML-and-Beyond.pdf</a></li>
<li><a href="Week-One-HTML-and-Beyond.ppt">Week-One-HTML-and-Beyond.ppt</a></li>
<li><a href="/examples/ca273/">Example files for this class</a></li>
<li><a href="http://www.alistapart.com/articles/doctype/" title="Go read the article on the ALA website">A 2002 Article from <em>A List Apart</em> on Doctypes from Jefferey Zeldman</a></li>
<li><a href="http://orderedlist.com/articles/structural-tags-in-html5" title="Read the post on Steve's Site: an Ordered List">A post from Steve Smith about Structural Tags in HTML5</a></li>
<li><a href="/cc/Basic-Anatomy-of-XHTML-Elements.png" title="See the large image used in the slideshow from class">Large illustration of the Basic Anatomy of HTML elements</a></li>
<li><a href="http://www.w3schools.com/tags/tag_meta.asp" title="Visit the W3Schools and learn more">W3Schools page on Meta Tags</a></li>
<li><a href="http://microformats.org/wiki/what-are-microformats" title="Read more at microformats.org">The Microfromats wiki page on what microformats are</a></li>
<li><a href="https://addons.mozilla.org/en-US/firefox/addon/4106" title="Operator add-on for Firefox that pulls Microformats from a page">Operator add-on for Firefox that pulls Microformats from a page</a></li>
</ul>

The list item is the parent element of the anchor tag, in the markup above.  To navigate from a child element to the parent element with jQuery is easy.

Navigating From Child to Parent Elements with jQuery .parent() Selector

Once we select the link we are going to use the .parent() selector to traverse the DOM up to the list item containing the link. Now our code looks like this:

$("li a[href$='pdf']").parent().addClass("pdf");

Notice that I also added li infront of the a selector because I don't want to accidently style any links that are not inside of list items.

Now that we have our selector for the pdf, we can easily modify it to work with other file extensions as well:

$("li a[href$='pdf']").parent().addClass("pdf");
$("li a[href$='ppt']").parent().addClass("ppt");
$("li a[href$='doc']").parent().addClass("doc");
$("li a[href$='xls']").parent().addClass("xls");

This will add the appropriate class to the appropriate list item (instead of the anchor tag).

Combining Attribute Filters to Select Images With Multiple File Extensions

When it comes to selecting and styling images, we want to be able to select any link in a list item that links to a .jpg, .gif, or  a .png file.  To do this we are going to use jQuery's ability to select or filter multiple attributes.

In practice, this looks like the following:

$("li a[href$='png'][href$='gif'][href$='jpg']").parent().addClass("picture");

This will allow us to select all the image types and add a class to the parent list item.

Determining Internal and External Links with jQuery

In the last part of this script I want to add a special class to a list item that contains a link to site besides my site (http://dabrook.org)

To accomplish this I used James Padolsey's :external extension for jQuery. You can find the extension about halfway down the page.  I won't repost his code here, so go check out his site for the code.  It is very straight forward and easy to implement.

Once you have his extension you can style external links just like this:

$("li a:external").parent().addClass("external");

The Complete Code

Here is the complete script (minus the :external extension) .

$("li a[href$='pdf']").parent().addClass("pdf");
$("li a[href$='ppt']").parent().addClass("ppt");
$("li a[href$='png'], li a[href$='gif'], li a[href$='jpg']").parent().addClass("picture");
$("li a:external").parent().addClass("external");

Comments

Leo Degan 02.16.09

Nice tutorial, jQuery is so powerful.

Zac Gordon 03.19.09

Thanks Leo!

Mike 03.27.09

Don’t need JQuery for this - it can be done with CSS alone:

HTML:

<ul><li>Link</li></ul>

CSS:

ul li a[href$=”.pdf”] {padding-left: 30px;background-image: url(“img/pdf.png”);background-repeat: no-repeat;
}

Repeat styles for each file type as necessary.

Zac Gordon 03.27.09

You make a good point Mike. 

I guess the only advantage with jQuery is that it has IE6 support.

Moderne Ausbildung 10.2.09

Great tutorial. I was looking for way to get the styling for external links working on IE6. Works fine!

Holler at this article

Comment Form

Welcome Section

Here you put the link to the RSS feeds, link to contact information and maybe a link to the about section.

Social Networking

DaBrook Tweets

  • Setting up github to share CodeIgniter source code with Springbrook web dev students. (03-09 11:37)
  • Just posted up information on the High School Web Conference next month http://bit.ly/9bkgXr (03-09 9:26)
  • Refresh Rockville information for the meetup tonight is live http://bit.ly/aQ3DOC (03-09 8:34)
  • Refresh Rockville: Project Management (Tues. night 7-8 @ MC) Will post details tomorrow (03-09 12:48)
  • Just setup Web 1 students at Springbrook with accounts on welovewebdesign.com ... look for cheap hosting for all web students soon! (03-08 9:06)
  • @mrbhachu Thanks about the jquery tutorial. I'll put it in the docket to make a new example for dropdown lists! (03-07 3:37)
  • Just submitted the reading list for the CMS course at MC: http://digwp.com/book/ http://bit.ly/boVz1n http://bit.ly/a3teWI (03-06 10:53)
  • A free event on preparing content for 508 accessibility! Great follow up on the accessibility lecture from last night http://bit.ly/bZRJUO (03-05 10:14)
  • The DaBrook.org forums are now live! http://dabrook.org/forums/ (03-04 11:04)
  • Great Advanced Web class tonight. I really enjoy the students this semester ... your forum is coming! (03-04 10:41)
  • First example of a student using the "Steal this theme" download from DaBrook - Nice work thrasher! http://bit.ly/bI4NIX (03-04 5:19)
  • It's amazing how fast the latest updates on my #ee grading module make grading. Love it! (03-04 5:04)
  • The MC Advanced Web class now has an archives page! http://bit.ly/aiRUhw (03-04 9:31)
  • Looking forward to subbing tonight for a class at CDIA working on WordPress theme development (03-03 2:57)
  • Web 1 students are working on their portfolios. Coming along nicely! (03-03 7:43)
  • First tweet for DaBrook.org Tweets http://dabrook.org (02-23 11:14)
  • What's current:

yeah, licensing is important, but I'm pretty cool with it.