jQuery: Better web programming

July 24, 2009

Point 1. jQuery emphasizes brevity.


The title pretty much states it all. Nearly everything I used to hate about making web pages fancy and functional is alleviated with a few lines of jQuery. With a few dollar signs here and there, you can accomplish all kinds of madness. For instance, consider the following:

$('.toHide').hide('slow');

Try me! [Scroll Down]

This little code bit slowly hides all elements on the page with the ‘toHide’ class. Conceptually, this is simple, but what have we truly accomplished? In a single line, we achieved implicit iteration [all .toHide elements], an opacity transition [hide], and timed animation [slow]. Much of the features of jQuery are available in one-liners as such. In fact, most methods return the jQuery object itself so that calls may be chained with righteousness.

$('.toHide').css('color','red').hide('slow');

Try me!

This turns all our toHide elements red and then immediately begins the hiding animation. Neat, huh? In all facets, it seems, jQuery emphasizes conciseness. From the jQuery function itself [$] to the way selectors work $(‘#myId’), everything is short. For code golfers, this should be a pleasant movement from document.getElementById(‘puke’).

Admittedly, this is but a small portion of what jQuery can do to make your life easier, and in the next post I’ll discuss the plugins architecture and its place in the mystical world of $(). If you thought firefox’s plugins were useful, you haven’t used jQuery’s.

Example:

  • Example list item 1
  • Example list item 2
  • Example list item 3

Example h3

tags:
posted in prog by Stefan Kendall

Follow comments via the RSS Feed | Leave a comment | Trackback URL

Leave Your Comment

 
Powered by Wordpress and MySQL. Theme by Shlomi Noach, openark.org