Imprint
Copyright 2012 Smashing Media GmbH, Freiburg, Germany
Version: October 2012 (P ublished in November, 2011)
ISBN: 9783943075168
Cover Design: Ricardo Gimenes
PR & Press: Stephan Poppe
eBook Strategy: Andrew Rogerson & Talita Telma Stckle
Technical Editing: Andrew Rogerson
Proofreading: Andrew Lobo, Iris Ljesnjanin
Idea & Concept: Smashing Media GmbH
About Smashing Magazine
Smashing Magazine is an online magazine dedicated to Web designers and developers worldwide. Its rigorous quality control and thorough editorial work has gathered a devoted community exceeding half a million subscribers, followers and fans. Each and every published article is carefully prepared, edited, reviewed and curated according to the high quality standards set in Smashing Magazine's own publishing policy. Smashing Magazine publishes articles on a daily basis with topics ranging from business, visual design, typography, front-end as well as back-end development, all the way to usability and user experience design. The magazine is and always has been a professional and independent online publication neither controlled nor influenced by any third parties, delivering content in the best interest of its readers. These guidelines are continually revised and updated to assure that the quality of the published content is never compromised.
About Smashing Media GmbH
Smashing Media GmbH is one of the world's leading online publishing companies in the field of Web design. Founded in 2009 by Sven Lennartz and Vitaly Friedman, the company's headquarters is situated in southern Germany, in the sunny city of Freiburg im Breisgau. Smashing Media's lead publication, Smashing Magazine, has gained worldwide attention since its emergence back in 2006, and is supported by the vast, global Smashing community and readership. Smashing Magazine had proven to be a trustworthy online source containing high quality articles on progressive design and coding techniques as well as recent developments in the Web design industry.
About this eBook
The explosion of JavaScript libraries and fra meworks within the front-end development scene has opened up the power of jQuery t o a far wider audience than ever before. What began from a necessity of front-end developers to upgrade JavaScript basic API took a new direction of unified implementation between browsers and to make it more compact in its syntax. Thanks to this development, it is possible to actually apply optimized scripts now. A script to find all links of a certain CSS class in a document and bind an event to them requires one single line of code instead of ten. Also, jQuery brings to the party its own API, featuring a host of functions, methods and syntactical peculiarities.
In this Smashing eBook #14: Mastering jQuery , you will learn how to combine JavaScript and jQuery with PHP and, specially, PHPs GD library to create an image manipulation tool to upload an image, then crop it and finally save the revised version to the server. In addition, you will be able to create your own bookmarklets, which are small JavaScript-powered applications in link form. Typically used to extend the functionality of the browser and to interact with Web services, bookmarklets allow you to post onto your own WordPress or Tumblr blog, submit any selected text to Google's search function, or modify a current CSS code within a browser just to cite a few!
Special attention is also given to jQuery plugins, which help save time and streamline development as well as allow programmers to avoid having to build every component from scratch. A good plugin saves countless development hours, whereas a bad plugin leads to bugs that must be fixed and so takes more of of your time than actually building the component from scratch. With the help of this eBook, you will get the best hints on how to choose which plugins are really worth considering for your projects and which ones you should avoid.
Table of Contents
Commonly Confused Bits Of jQuery
Andy Croxall
The explosion of JavaScript libraries and frameworks such as jQuery onto the front-end development scene has opened up the power of JavaScript to a far wider audience than ever before. It was born of the need expressed by a crescendo of screaming by front-end developers who were fast running out of hair to pull out to improve JavaScripts somewhat primitive API, to make up for the lack of unified implementation across browsers and to make it more compact in its syntax.
All of which means that, unless you have some odd grudge against jQuery, those days are gone you can actually get stuff done now. A script to find all links of a certain CSS class in a document and bind an event to them now requires one line of code, not 10. To power this, jQuery brings to the party its own API, featuring a host of functions, methods and syntactical peculiarities. Some are confused or appear similar to each other but actually differ in some way. This article clears up some of these confusions.
1..parent() vs..parents() vs..closest()
All three of these methods are concerned with navigating upwards through the DOM, above the element(s) returned by the selector, and matching certain parents or, beyond them, ancestors. But they differ from each other in ways that make them each uniquely useful.
parent(selector)
This simply matches the one immediate parent of the element(s). It can take a selector, which can be useful for matching the parent only in certain situations. For example:
$('span#mySpan').parent().css('background', '#f90');
$('p').parent('div.large').css('background', '#f90');
The first line gives the parent of #mySpan . The second does the same for parents of all
tags, provided that the parent is a div and has the class large .
Tip: the ability to limit the reach of methods like the one in the second line is a common feature of jQuery. The majority of DOM manipulation methods allow you to specify a selector in this way, so its not unique to parent() .
parents(selector)
This acts in much the same way as parent() , except that it is not restricted to just one level above the matched element(s). That is, it can return multiple ancestors . So, for example:
$('li.nav').parents('li'); //for each LI that has the class nav, go find all its parents/ancestors that are also LIs
This says that for each
- that has the class nav , return all its parents/ancestors that are also
- s. This could be useful in a multi-level navigation tree, like the following:
- Link 1
- Sub link 1.1
- Sub link 1.2
- Sub link 1.3
- Link 2
Imagine we wanted to color every third-generation
- in that tree orange. Simple:
$('#nav li').each(function() {
if ($(this).parents('#nav li').length == 2)
$(this).css('color', '#f90');
});
This translates like so: for every
- found in #nav (hence our each() loop), whether its a direct child or not, see how many
- parents/ancestors are above it within #nav . If the number is two, then this
- must be on level three, in which case color.
closest(selector)
This is a bit of a well-kept secret, but very useful. It works like parents() , except that it returns only one parent/ancestor. In my experience, youll normally want to check for the existence of one particular element in an elements ancestry, not a whole bunch of them, so I tend to use this more than parents() . Say we wanted to know whether an element was a descendant of another, however deep in the family tree:
if ($('#element1').closest('#element2').length == 1)