jQuery provides a great way for web developers to build cross-browser JavaScript enabled applications. This article aims at getting you up and running with jQuery as quickly as possible without assuming any prior jQuery or JavaScript knowledge.
If you are interested in getting started with jQuery but don't know where to begin, you are in the right place.
Before we do anything, we need to get our paws on the jQuery library. There are two ways to do this.
This is the path with which all jQuery goodness flows through. Before you can truly take advantage of the power of jQuery, you first must find the HTML or CSS element that you want to start processing on.
The jQuery() function takes a selector/element/etc... that will be used later for processing.
The nice folks at jQuery also provided a shortcut for jQuery() which is $(). All of the examples you see online will contain the shorthand version.
I'll get into this in more detail in a later post.
$(document).ready() will likely be in every one of your jQuery enabled web applications.
$(document) creates a jQuery wrapper around the document object (also known as the DOM). This will allow you to call jQuery API methods on the web document.
The ready() method will detect when a page has loaded and is ready to use. This will occur after all of the elements are loaded into the DOM and before any of the images are loaded.
ready() takes a handler as its parameter which contains the method to be called once the DOM is loaded. You can specify an actual method to be called here, or simply use an anonymous method (which is the de facto standard).
$(document).ready(function() {
//This is where your code goes
});
Now that we have the basics down, it is time to see jQuery in action. Here is a really simple HTML 5 skeleton that loads in jQuery from Microsoft's CDN and pops up an alert() window to let us know jQuery is loaded.
Congratulations! You have just created your very first jQuery application.
Before you move on from here, it might be best to take a quick look at the jQuery documentation just to get a feel for what all jQuery is capable of and how the syntax looks.
This part is completely optional, but chances are you'll find yourself here really soon so it might be best to get familiar with the docs sooner rather than later.
Now that we have jQuery up and running on our website, it would be super nice to start doing meaningful work. After all, we didn't go through all of this just to display a dumb pop-up window, right?
In one of my next articles, I'll go over the single most important piece of functionality in jQuery, the selector.