
Over the past few years, jQuery has grown into an important element of JavaScript and web development. Although some people argue that this cross-platform JavaScript library is rapidly outliving its usefulness, its ubiquity means it's actually not going anywhere soon; if you’re interested in JavaScript, it’s well worth your time to learn enough jQuery to be useful. But how much do you need to know to land a job? Many people claim you can pick up jQuery before learning JavaScript. While that might work for freelance web developers, if you're trying to land a full-time job as a web developer, and you're in competition with other candidates, you want to make sure you know JavaScript as well as jQuery. You don't need to be a full-on JavaScript ninja before starting down the jQuery path, but you certainly want to know the language, especially how to use callback functions. So let's start there.
Learn JavaScript
In addition to knowing the basics of JavaScript (what a variable is, how to create and call a function, and so on), you should know how to create functions that can serve as callback functions. In addition, you should understand two important facets of the language: how to manage arrays, and how the “this” variable is used in a callback function. Why should you know how to manage arrays? When you search for elements in your HTML document, even if you find one element (or zero elements, for that matter), you nonetheless get back an object that contains an array of elements. In the zero-element scenario, your result is an object with an empty array; you don't get back a null variable, as some people might expect. For example, jQuery calls for events typically look like this:jQuery('.clickable').on('click', function() {This short bit of code demonstrates several things. First, the jQuery ('.clickable') call returns a list of elements, specifically any elements that have class clickable. Next, this code calls the “on” function for that single array. (It does not call “on” for each element in the array, which is important to understand.) This code provides the “on” function with two arguments: the name of the event to handle (click) and a function to call in response to that event. Look at how there are two arguments given to “on,” and that the second argument is a function. The code for jQuery's “on” function assigns this event to all the elements it finds that are of class .clickable. How does it know which elements? The object you got back from the first call to jQuery returned an object that includes a member function called on. The code inside that member function has access to the object, including the array that contains the selected elements found in the first call. Understanding the mechanics of this is vital to truly understanding jQuery. Finally, the callback function will get called for each element that matches the selector. It might be zero times, one time, or many times. Inside the callback function is the “this” variable. In JavaScript, the “this” variable can be messy: what it refers to depends on how the function containing it is called. jQuery carefully manages this for you, so that your callback assuredly refers to the current element.console.log(jQuery(this).attr('id'));
});
Study jQuery's Documentation
With a solid beginner-to-intermediate understanding of JavaScript, you can easily master jQuery. But here's the catch: By itself, jQuery really isn't all that big. You can read through the entire documentation in a few hours. The key to learning jQuery is figuring out how to accomplish tasks in jQuery. As such:- Read the documentation.
- Familiarize yourself with the functions available. For example, know that there are different ways to find the parent of an element, such as parents() and closest(), and understand the difference.
- Learn how to chain your commands; most functions in jQuery return the same list of elements you started with, which means you can continue processing by simply tacking on more functions preceded by a dot, as in: $('div').addClass('selected').show();
- Understand when the document is finished loading and how to write code that safely access the elements only after the document is loaded. There are several ways to accomplish this.
- Learn CSS selectors. This is vital, because it's through CSS selectors that you use jQuery to locate your elements.
- Understand HTML best practices, and when to use IDs versus classes. (Yes, that's a separate issue from jQuery, but it impacts how you use jQuery.)