Quick Tip: Dissecting jQuery – Filters
videos

Quick Tip: Dissecting jQuery – Filters

Tutorial Details
  • Topic: The jQuery Source
  • Estimated Difficulty: Intermediate
  • Tutorial Format: 11 Minute Screencast
Download Source Files

Sporadically, over the course of each month, we’ll post a “Dissecting jQuery” video quick tip. The idea behind these is that we’ll take a single chunk of the jQuery source at a time, break it down, and determine exactly what’s going on under the hood, so to speak. Then, with that knowledge, we’ll learn how to better utilize the library in our coding. Today, we’ll review filters.


Video Tut


jQuery’s Source for the :hidden Filter

jQuery.expr.filters.hidden = function( elem ) {
		var width = elem.offsetWidth, height = elem.offsetHeight,
			skip = elem.nodeName.toLowerCase() === "tr";

		return width === 0 && height === 0 && !skip ?
			true :
			width > 0 && height > 0 && !skip ?
				false :
				jQuery.curCSS(elem, "display") === "none";
	};


The :visible Filter

Quite cleverly, the :visible filter only needs to call the hidden method, and return the reciprocal.

jQuery.expr.filters.visible = function( elem ) {
	return !jQuery.expr.filters.hidden( elem );
};

Hint: Search for filters: and setFilters: within the jQuery source code to view a listing of other helpful filters that are available to you.


Harnessing this Knowledge to Extend jQuery

<script>
	$('p:first').data('info', 'value'); // populates $'s data object to have something to work with
	
	$.extend(
		jQuery.expr[":"], {
			block: function(elem) {
				return $(elem).css("display") === "block";
			},
			
			hasData : function(elem) {				
				return !$.isEmptyObject( $(elem).data() );
			}
		}
	);
	
	$("p:hasData").text("has data"); // grabs paras that have data attached
	$("p:block").text("are block level"); // grabs only paragraphs that have a display of "block"
</script>

Note: jQuery.expr[':'] is simply an alias for jQuery.expr.filters.


Stay tuned. In future episodes, we’ll continue to slice out more chunks of the jQuery source, and dissect them!

Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • razvantim

    Great tutorial. I must say that being a good javascript developer is not in using libraries, but understanting them is most important.

  • Shadow

    Hey, thanks for this, this looks promising, but please keep this up, don’t abandon it after 2 months :D

  • daniel

    thanks Jeff! I’m looking forward to seeing more of these quick tips! the jquery source code can be quite dauting I think, but having you guiding us (me) really helps ;)

    • http://www.jenswebstek.nl/zite/index.php/dev jenski

      I second that,
      this looks like a great series tnx

  • http://dmitry-scriptin.blogspot.com/ Scriptin

    Nice idea to dessect the insides of that framework. That’ll help me to figure out if I should use it or not in my future projects.

    It also would be nice to see similar tutorials for other frameworks like Prototype.js and MooTools to compare them.

  • http://qmmr.pl qmmr

    Very nice, I was curious how to dissect jQuery :)

  • ben

    Dang no more videos on Screenr? Can’t watch these tutorials at work anymore! (youtube blocked)

    • http://www.jeffrey-way.com Jeffrey Way
      Author

      If the video will be less than 5 minutes, I record on Screenr — otherwise, it won’t fit. :(

  • http://www.iguoguo.net iguoguo

    I use jquery every day , but I had never used filters.

  • http://twitter.com/korenbr Koren Berman

    Fantastic tutorial, Jeffrey. I can think of a number of ways that would come handy. I think many jquery coders rarely or never write extensions to jquery (and if we do use them, it’s through a plugin).

    I’m not sure whether that’s a viable option, but an extension that could format an instance of a word or sentence would be very useful (i.e. a simplified version of preg_replace). For example, that’d be useful when you want to format occurrences of ampersands to display them in a italic Baskerville. It’s not a selector just another function.

    Anyways great job, I’m looking forward to more of those.

  • http://ourownchange.com brendan

    I would like to see WordPress tutorials like this. :)

    It’s interesting to see the behind the scenes and have it explained. I know personally I don’t feel strong enough in my knowledge of JavaScript to just going poking around.

    Thanks for the tuts!

  • http://datasplash.co.uk Darren Lunn

    Great stuff Jeff, very good tutorial for a JQuery beginner like me, not only in the how to use use it, but just as importantly in how to find the relevant pieces within the source file and understand what it’s actually doing.

    Very much looking forward to this series, I hope you cover some of the JQuery UI too, that’s losing me at the moment, but probably cos it’s early days..

    • http://www.jeffrey-way.com Jeffrey Way
      Author

      Hey Darren –

      Thx! I honestly probably won’t be covering jQuery UI in this set. I’ll leave that to someone who is much more knowledegable in that area. :)

  • http://bit.ly/cLZXGi Julian

    Love where this is going! Looking forward to future series, particularly the Ajax part. Will you be coving Ajax methods and how to use them?

  • http://www.thedesigngnome.com Max Luzuriaga

    Great video, I love dissecting other’s code :) it makes me feel more accomplished, like I myself wrote it!

  • http://mustafanamoglu.com/ Mustafa Namoglu

    Thanks Jeffrey, love to watch such jquery related tutorials. It is a bit diffucult learn from books.

  • http://www.designmango.com DesignMango

    Great video, really useful for anyone learining!

  • http://omarabid.com Abid Omar

    Hey Jeff,
    I’m not quite interested in Nettuts plus tutorials, but want to watch these screencasts. Unluckily, you always put them on Youtube, so I can’t watch, it’s banned here.

    I found a download link for premium members, so I signed up for premium membership. Unluckily (again) I don’t find out how to DOWNLOAD, I want the files to be on my desk and watch them. Something big, just open up and keep loading.

    It’s noted “download” so shouldn’t I be able to download the fu*%^$ùg video????? Thanks

    • http://www.jeffrey-way.com Jeffrey Way
      Author

      Hey Abid, Just right-click on the “View Screencast” link (in the Premium section), and choose “Save to Computer.”

      • http://omarabid.com Abid Omar

        Thanks Jeff,

        found download links here http://tutsplus.com/plus-content/nettuts/ too.
        Between, I got an eye on the premium content and all I can say is I retreat of what I have said earlier.
        Thanks, lot of value considering what I have paid and I’m also interested on all tuts sites now :)

  • Kris

    What theme are you using for Espresso Jeffrey?

    By the way you said you were gonna open it in Textmate. I saw the interface and thought maybe Textmate 2.0 was out. hahaha

    • http://www.jeffrey-way.com Jeffrey Way
      Author

      It’s called Cappuccino. Haha – yeah, I caught that TextMate blunder when I was editing the video. :)

  • http://jayphen.com Jayphen

    Do you never sleep? ;)

  • vaff

    Hmn function isnt working for me :/

  • http://www.wannymiarelli.it Wanny

    Great !

  • http://blueco.ir/ طراحی سایت و سئو

    its very useful for me tnx a lot buddy