My Experience with JQuery

I had a project recently where I needed to make an online contact management website with similar functionality to Outlook and ACT!.

I wrote all the javascript from scratch for the first version of the site. I ended up writing over 1000 lines of code to add various features, such as ajax search, autocomplete, input masking, and security.

The code was getting too complex to manage so I decided to look for an alternative. I came across several javascript libraries, including mootools and YUI. I tried each of these, but the learning curve was too steep. I would spend hours just trying to implement a simple feature. This ended up being worse than my custom code.

The next library I tried was jQuery. At first this looked just as bad as the others, but after fooling around with it for a few hours, I fell in love. The language is very simple to understand. It all consists of a single $() function that replaces and improves document.getElementById(). Here are some examples.

//Select the element with id "test"
$("#test")
//Select elements with a class of "style1"
$(".style1")
//Select all <p> and <blockquote> tags
$("p, blockquote")
//Select all input fields inside of a form
$("form input")

Most selections that works in CSS work with the $() function. All jQuery functions start with a selection like this. Different functions are chained together after this to perform tasks. For example:

//Change the color of all <p> tags to red and 
//add the word "hello" to the end of each.
$("p").css('color','red').append('hello');

In the previous example, this paragraph would turn into:

In the previous example, this paragraph would turn into:hello

The built-in functions can acomplish a lot, but the real power of jQuery comes from the plugins. jQuery is one of the few frameworks that completely supports 3rd part plugins. Some common ones add things like tabs, accordions, autocomplete, tooltips, rich text editing, and so on.

All you need to use one of these plugins is to include the script. Here's an example using the tooltips plugin.

<script type='text/javascript' src='jquery.js'></script>
<script type='text/javascript' src='jquery.tooltips.js'></script>
<script type='text/javascript'>
//Add a tooltip to every element with class 'tooltip'
$(".tooltip").tooltip();

//Add a tooltip with custom options
$(".custom").tooltip({ 
    track: true, //follow the mouse
    delay: 0, //show tooltip instantly
    fade: 250 //fade tooltip out
});
</script>

Here's a list of the plugins I used for my contact management site:

  • Meio Mask - This conforms entered input to a specified format or "mask". For example, if you enter a phone number, no matter how you enter it, it will always look like "(123) 456 - 7890". I used this to standardize entered data and to make searching easier.
  • Tooltip - This plugin lets you add tooltips to any element. They can be anywhere from simple text to advanced layouts with images. I used this on any icons to provide a nice user interface.
  • Autocomplete - Provides an easy way to add autocomplete fields to your site. I used this for my ajax search and to suggest city and country names to reduce misspellings and standardize data.
  • Editable Combo Box - This lets you type your own value in a select box. I used this along with some server side scripting to allow users to easily add options to drop down menus.
  • Date Picker - This creates pop-up calendars for entering dates. I used this on all my date fields to provide an alternate way to enter data.
  • Tabs - This creates an element with tabs containing different content. I used this to fit more information in a single place.

The uncompressed jQuery framework with these plugins is close to 200kb. This is a little much to load for every page. Luckily, there are ways to reduce the size by more than half. The easiest way is to use the Javascript Minifier. This removes all comments and unnecessary spaces and linefeeds.

5 comments:

nwhite said...

I'm happy you found a solution that worked for you. I have nothing against JQuery as it seems to be the tool of choice for many. You mentioned MooTools being very difficult to use. Can you elaborate on what made it so difficult? Mootools use very much the same syntax as JQuery. Not trying to convert, just trying to understand the weaknesses.

Jeremy Dorn said...

@Nathan

Thanks for the comment. Maybe "very difficult" was a bit harsh. I just felt like jQuery was more intuitive than mootools.

I really like the hundreds of 3rd party plugins available for jQuery. There are 4 or 5 plugins for adding tabs, 3 autocomplete plugins, at least 3 tooltip plugins, etc. I like the ability to choose which one I want to use.

The one thing mootools does better than jquery is the download process. I really like only having the functions and features I need in a single file. I ended up doing a similar thing with jQuery by combining all the plugins I use in a single file.

Anonymous said...

Hi jeremy, tahnks for the link to meioMask, i really appreciate. It will look odd because my plugin is for jQuery and ill tell you that i preffer mootools. And the only reason that i've made this plugin using jquery is because jQuery has more, and some times, better plugins than mootools. And thats why i use it on the projects i'm doing right now. But you should really try mootools a little more. By using jQuery u'll see that sometimes you'll need an array function that it doesn't provide, or a string function... mootools propably will have because it extends the javascript objects with great functionality (you don't need to use the $ function everytime).

Well that's a big comment, have a nice day and thanks again!

Addison said...

Wonderful information you shared in this post. Thanks a lot for sharing.
Site CSS Templates

Austin Brove said...

Brilliant! Your talent is really cool
Free Web Templates