The Ajax Experience: jQuery Toolkit

jquery I went to The Ajax Experience with high expectations of catching some great tips regarding development in an Ajax environment. At the same time, I was sure of my previous decision with the use of Prototype and Script.aculo.us was as good as it gets (without diving into the widgetized world...e.g. Dojo). I attended John Resig's presentation on jQuery and I became a convert.

John's presentation was less of a presentation and more of a Q&A Demonstration, which suited me fine. As soon as I knew where to download the code, I popped open my laptop and started fiddling around with the toolkit - passively paying attention to the questions and answers, as they tended to be extremely basic questions...you see, jQuery is pretty darn intuitive.

jQuery's mantra is "Find stuff and do stuff to it"

Yeah, I wasn't converted because jQuery was the first toolkit to support chaining and that it executes it nicely. Nor was I converted because of its extensive plugin library. Nope. My conversion was the effecient findability of elements within the DOM! This is what really makes jQuery ballsy. The toolkit was built with findability in mind using already established standards! jQuery fully supports CSS1, CSS2, CSS3, and basic XPath when hunting for elements. For example:

Lets start with something simple:
Say I wanted to find all elements within the page that had the class: bork and hide them, I'd do:

JavaScript:
  1. $('.bork').hide();

Alright, say I wanted to find all anchor tags with the target set to _blank and add the class whee to it:

JavaScript:
  1. $('a[@target=_blank]').addClass('whee');

Now, lets say I want to find all anchor tags with the target set to _blank and add the class whee to them AND append (opens in a new window) as a sibling to the link itself.

JavaScript:
  1. $('a[@target=_blank]').each(
  2.   function(){
  3.     $(this).addClass('whee');
  4.     $(this).after('(Opens in a new window)');
  5.   }
  6. );

Now, if I knew I was going to use the above a whole lot all over hell's half acre, I could create a jQuery plugin that can be chained! Here's how I'd create that feature and allow for the passing of class name:

JavaScript:
  1. jQuery.fn.opensInNewWindow = function(classname){
  2.     return this.each(
  3.         function(){
  4.                         $(this).addClass(classname);
  5.                         $(this).after('(Opens in a new window)');
  6.         }
  7.     );
  8. };

Now, when I want to put Opens in a new window on a series of elements, I can do so with my newly created plugin:

JavaScript:
  1. $('a[@target=_blank]').opensInNewWindow('whee');
  2.  
  3. //I can do this for ANY element I want even if it isn't a link
  4. $('span.bork').opensInNewWindow('zomg');
  5. $('div#w00t ul.nav').opensInNewWindow('roflcopter');

Now, to make use of the chainability, you can write the plugin more simply than what I did up above. You can do this: (thanks to malsup, a commenter on this article)

JavaScript:
  1. jQuery.fn.opensInNewWindow = function(classname){
  2.     return this.each(
  3.         function(){
  4.       $(this).addClass(classname).after('(Opens in a new window)');
  5.         }
  6.     );
  7. };

Cake walk. As your proficiency in finding objects within a page increases, your code will become shorter.

jQuery currently has a very active community and mailing list (averaging at about 90 posts per day) so if you have questions regarding the toolkit, you can become a member and ask away... In addition, there's a jQuery blog where John posts comparison code showing the difference between jQuery and other available toolkits. (here's a Zebra striping example)

If you are still on the hunt for a JavaScript toolkit for Ajax, DOM manipulation, etc or you find your toolkit of choice too clunky, give jQuery a shot. You'll be pleased with the results.

Discuss This Article


8 Responses to “The Ajax Experience: jQuery Toolkit”

  1. pingback pingback:
    BorkWeb » Visual JQuery

    [...] After my excitement about jQuery since The Ajax Experience, one of my fellow developers at PSU has been checking the toolkit out. In her searches for documentation found Visual jQuery, a nice graphical/textual categorized API for the jQuery toolkit! Its a pretty snazzy learning tool if you aren’t already familiar with all the functions jQuery has to offer (found in the jQuery API). [...]

    Reply to this comment.
    1
  2. Avatarmalsup

    You’re forgetting that great chainability! This:

    $(this).addClass(classname);
    $(this).after(’(Opens in a new window)’);

    is more simply written as:

    $(this).addClass(classname).after(’(Opens in a new window)’);

    Reply to this comment.
    2
  3. AvatarMatt
    Author Comment

    OOooo good point :D I guess I’m still new at all this jQuery stuff. I’ll get there ;)

    Reply to this comment.
    3
  4. AvatarJohn Resig

    Hi Matt - Glad you liked the presentation!

    Just a quick optimization on the code that you’ve outlined, you can reduce what you’ve done to:

    jQuery.fn.opensInNewWindow = function(classname){
    return this.addClass(classname).after(’(Opens in a new window)’);
    };

    Short-and-sweet! Let me know if you have any questions.

    Reply to this comment.
    4
  5. pingback pingback:
    BorkWeb » JavaScript Shell

    [...] This little beauty, once you’ve added the bookmark to your browser allows you to open a JavaScript Shell for any page you happen to be on! The shell lets you enter JavaScript from command-line to manipulate the page, trigger functions, analyze properties, etc. All libraries that the site has loaded are available within the Shell…so, if you use jQuery or some other toolkit, all defined functions and plugins are usable. [...]

    Reply to this comment.
    5
  6. pingback pingback:
    No Sheep » 2006 in Review: Personal Top 10

    [...] 6 - Was introduced to JQuery After attending the Ajax Experience, Matt introduced me to JQuery. This is the ideal JavaScript toolkit for how I like code to be structured. This new technology in my toolkit is already greatly effecting what I am capable of creating and maintaining. As I become more proficient, I expect my love of JQuery to grow even further. [...]

    Reply to this comment.
    6
  7. pingback pingback:
    BorkWeb » JSMin: Javascript Compression

    [...] As I’ve been creating more complex Javascript applications, the file size has been increasing (although the size is greatly reduced, thanks to jQuery). And, as a careful programmer should, I place comments all over my Javascript code so I don’t draw too many blanks while updating/debugging…well, those comments tend to bloat the file size, as does the whitespace. The stripping of those elements alone has dropped the file-size of a number of my scripts by 40-50%! Thats huge. What previously was an 8K file goes down to 4K. Awesome. [...]

    Reply to this comment.
    7
  8. pingback pingback:
    BorkWeb » jQuery 1.2 Released!

    [...] is a major release for the JavaScript library that I have grown to love. Before I list the new features, it is important to note what functionality has been removed [...]

    Reply to this comment.
    8

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Comment Preview:

 (4857) - jquery window (462) - jquery (103) - jquery open window (86) - jquery ajax examples (83) - jQuery Sample (75) - jquery change class name (75) - jquery toolkit (69) - jquery open new window (69) - jquery samples (61) - jquery change classname (57) - jquery get window size (56) - jquery window.open (51) - jquery dojo (48) - jquery window size (41) - jquery opener (41) - jquery window open (39) - window jquery (37) - jquery get classname (33) - jquery get class name (33) - jquery new window (32) - jquery screen resolution (31) - jquery get class (29) - jquery screen size (27) - jquery comparison (26) - jquery class name (26) - jquery set class name (26) - jquery ajax sample (24) - jquery get element by class name (24) - jquery get element by class (22) - open new window jquery (22) - jquery getClass (21) - jquery getelement (21) - dojo jquery (20) - jquery find class (20) - jquery set class (20) - jquery set classname (20) - jquery window.opener (20) - jquery change class (18) - jquery get elements by class name (18) - jquery ajax example (17) - window.open jquery (17) - jquery ajax window (16) - jquery popup window (16) - jquery classname (14) - jquery get element by classname (14) - jquery find by class (14) - jquery resize window (14) - JQuery Ajax Tutorial (13) - jquery dragable (13) - jquery window opener (12) - jquery toolkits (12) - open new window with jquery (12) - jquery get elements by classname (12) - jquery get element by name (12) - jquery getelements (11) - jquery open popup (11) - jquery get screen resolution (11) - jquery openwindow (10) - jquery get screen size (10) - ajax Toolkit Comparison (9) - jquery find by class name (9) - jquery get elements by class (9) - jquery open in new window (9) - get class name jquery (9) - window.opener jquery (9) - jquery screen width (9) - jquery get width (8) - jquery setclass (8) - jquery create window (8) - dojo or jquery (8) - jquery dragable window (8) - jquery addClass (8) - jquery $(window) (8) - jquery window resize (8) - jquery ajax popup (8) - ajax jquery examples (7) - jquery or dojo (7) - jquery target blank (7) - className jquery (7) - jquery open windows (7) - get elements by class name jquery (7) - opener jquery (7) - ajax jquery tutorial (6) - open window jquery (6) - dojo jquery comparison (6) - jquery find element by class name (6) - jquery window example (6) - jquery window examples (6) - jquery ajax link (6) - getClass jquery (6) - jquery popup (6) - jquery window width (5) - ajax jquery example (5) - jquery dojo comparison (5) - comparison jquery dojo (5) - jquery ajax toolkit (5) - jQuery.ajax examples (5) - jquery set window size (5) - get element by class name jquery (5) - jquery get elements by name (5) - jquery get by classname (5) - jquery getstyle (5) - jquery change window size (5) - jquery class change (5) - jquery get screen width (5) - jquery window class (4) - jquery get window width (4) - jQuery after (4) - jquery ajax get (4) - jquery examples ajax (4) - jquery open a window (4) - get classname jquery (4) - sample jquery (4) - jquery find by classname (4) - window with jquery (4) - jquery get element class name (4) - jquery changeclass (4) - jQuery.ajax example (4) - jquery get element class (4) - jquery get element (4) - jQuery windows size (4) - jquery windowing (4) - jquery find class name (4) - jquery tool kit (4) - jquery get file size (4) - change class name with jquery (4) - jquery open popup window (4) - jquery popup opener (4) - jquery popup window example (4) - ajax window size (4) - jquery get all elements by className (4) - ajax jquery popup window (4) - jquery screensize (4) - jquery new window size (4) - jquery newwindow (3) - jquery find (3) - ajax jquery sample (3) - jquery ajax samples (3) - window open jquery (3) - jquery find window size (3) - jquery find classname (3) - window size jquery (3) - jquery this class name (3) - jquery get by class (3) - jquery get window height (3) - jquery window ajax (3) - jquery windows (3) - ajax get window size (3) - jquery open new page (3) -