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!

Add Comment

Discussion 26 Comments

  1. razvantim says:

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

  2. Shadow says:

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

  3. daniel says:

    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 ;)

  4. Scriptin says:

    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.

  5. qmmr says:

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

  6. ben says:

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

  7. iguoguo says:

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

  8. Koren Berman says:

    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.

  9. brendan says:

    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!

  10. Darren Lunn says:

    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..

    • Jeffrey Way says:
      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. :)

  11. Julian says:

    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?

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

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

  14. DesignMango says:

    Great video, really useful for anyone learining!

  15. Abid Omar says:

    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

  16. Kris says:

    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

  17. Jayphen says:

    Do you never sleep? ;)

  18. vaff says:

    Hmn function isnt working for me :/

  19. its very useful for me tnx a lot buddy

Add a Comment

To add a code snippet to your comment, please wrap your code like so: <pre name="code" class="html">YOUR CODE</pre>. You can replace the class name with "js," "css," "sql," or "php." If there are any "<" or ">" within your code, please search and replace them with: &lt; and &gt; respectively.