Just a quick couple of examples for one of jQuery’s underused functions. The $.filter() function is probably not used as much as more popular functions such as $.find() because it offers a slower speed according to jsperf.


$.filter() Example 1

Filter all the hyperlinks which target MS Word Documents and PDF’s then attach an event which fires off a Google Analytics custom event tracking.

//checking specific file types
     return /(.pdf|.doc)$/.test($(this).attr('href'));
}).bind('click', function()
     _gaq.push(['_trackEvent', window.location.pathname, 'Download', $(this).attr('href')]);

$.filter() Example 2

Filter all hyperlinks which target PNG and JPG images… add a nice red border to them.

//get all image links
  return /(jpg|png)$/.test($(this).attr('href'))
   $(v).css('border','2px solid red');
Sam Deering is a Front-end Web Developer who specialises in JavaScript & jQuery. Sam is driven and passionate about sharing his knowledge to educate others.

Free Guide:

How to Choose the Right Charting Library for Your Application

How do you make sure that the charting library you choose has everything you need? Sign up to receive this detailed guide from FusionCharts, which explores all the factors you need to consider before making the decision.

No Reader comments

Special Offer
Free course!

Git into it! Bonus course Introduction to Git is yours when you take up a free 14 day SitePoint Premium trial.