The Lodge is members-only design/dev videos and Office Hours.

Next Office Hours Session: "Implementing an SVG Icon System" Nov 30 - 6:00 PM Eastern

Automatically Discover Document Links And Apply Class

Last updated on:
$('a[href]').each(function() {
   if((C = $(this).attr('href').match(/[.](doc|xls|pdf)$/))) {

This will look through every a element on the page. If the href attribute of it has a .doc, .xls, or .pdf in it, it will apply the appropriate class name to it (e.g. class="doc")


  1. Mike
    Permalink to comment#

    Works like a champ!

  2. Nertus
    Permalink to comment#


  3. Tem Corner
    Permalink to comment#

    Although IE6 would degrade to a normal link, this can be done by CSS.

    a[href$='.doc'] { padding-right:16px; background: transparent url(images/doc.png) no-repeat top right; }

    • Ahmed
      Permalink to comment#

      yes, that’s right css way is better than jquery way(personal opinion) :).

Leave a Comment

Posting Code

We highly encourage you to post problematic HTML/CSS/JavaScript over on CodePen and include the link in your post. It's much easier to see, understand, and help with when you do that.

Markdown is supported, so you can write inline code like `<div>this</div>` or multiline blocks of code in in triple backtick fences like this:

  function example() {
    element.innerHTML = "<div>code</div>";

There's a whole bunch of content on CSS-Tricks.

Search for Stuff   •   Browse the Archives

Get the Newsletter ... or get the RSS feed