jQuery - A quick start guideMonday 7th September 2009 at 2:14 am
This article is designed to introduce a complete beginner to jQuery. jQuery is not a new language nor is it an independent platform. It's a Javascript library designed to make it easier for developers to 'do more, write less.' So it's written in Javascript, meaning it follows the Javascript syntax. I assume that you are familiar with the basic terminology of Object Oriented programming - ie. Classes, objects, methods etc. The advantages
The disadvantages
Including the librarySo, the first thing you'll need to do is download the latest library from the jQuery website. You'll then need to include it in the section of your HTML file:
You will need a place to write your javascript code, so create a script tag:
The DOM (Document Object Model)Normally, javascript is executed immediately by the browser as it interprets the HTML page, however often it's preferable to execute javascript once everything on the page has loaded. jQuery solves this problem by listening for the DOM to be ready:
You can read more about the DOM here. SelectorsSelecting elements in your page is easy with jQuery. There a a whole range of selectors to use, the simplest way is to use a CSS style selector:
This will select the HTML element with ID 'myElement', so for example this div tag:
You can also select a whole series of elements, for example:
This will select all elements on your page with the Class 'myElements'. Remember, good practice says that you should not assign the same ID to multiple elements, but you can assign the same class to multiple elements. You might be wondering that the $ symbol is for, this is just a reference to the main jQuery class - it's fine to use if you don't plan on including any other libraries such as Prototype, but if you do plan on using other libraries too you can use the following syntax:
MethodsTo modify or apply a behavior to an element you use a method. There are lots of built in methods that come with jQuery. For example, the CSS method adjusts the CSS style of the element, so for example:
This statement effectively introduces this CSS:
There are more interesting methods that come with the basic jQuery library too, one such method is the load() method. This is one of jQuery's AJAX functions that loads the contents of an external file into an element, so for example:
This statement would replace the contents of the element with ID 'myElement' with the contents of the file 'myfile.txt', without reloading the whole page. This is an excellent example of how jQuery can save you a whole lot of time/code. CallbacksCallbacks are functions executed after the parent function has finished executing, for example:
So once the new AJAX content has finished loading the unnamed function will executing, alerting the user it's finished. ListenersUsually jQuery is used to enhance user interaction with the webpage, when a user clicks on an element for instance. jQuery uses 'listeners' to detect user interaction with elements on the page, so for example:
Now, when a user clicks on the element with ID 'myElement' they will trigger the alert 'You clicked me!'. PluginsPlugins usually extend the jQuery base class. So for instance the Cycle Plugin makes it easy to create fading image slideshows. First you need to download and include the Cycle plugin after your include for the main jQuery class, so your head section now looks like this:
Next you need to create the HTML elements for your slideshow:
Our container element #slideshow contains three image elements that make up the three slides in our slideshow. Now, we apply the Cycle method to the container element:
That's it! You've created a slideshow in a few simple steps. DebuggingDebugging Javascript can be difficult because as soon as one statement breaks, it will stop executing script and not always throw an error. This task can be made much easier by using Firebug - a plugin for firefox that displays errors in javascript code that the browser is executing. SummaryThis guide is designed to show you the very basics of how to get started with jQuery. You don't need to be an expert in Javascript to get started and begin making complex interactions with relative ease. The examples in this article are purposely basic, but use the jQuery Docs website to find out more about the methods mentioned and discover the massive range of other methods at your disposal. A couple of hours spent getting to grips with jQuery will save you hours later! |
Latest 5 articlesTips for choosing a web hosting provider 5 ways you can benefit from bulk SMS marketing A beginners guide to web hosting and domain names Wardrobe, Great Dunmow - website launched Web-based system example: A library management system Article ArchiveMarch 2009 April 2009 May 2009 June 2009 July 2009 September 2009 October 2009 November 2009 April 2010 |