What’s New in jQuery 1.7

Craig Buckler

The web’s most popular JavaScript library has been updated. jQuery 1.7 can now be downloaded from docs.jquery.com/Downloading_jQuery. Alternatively, if you prefer a CDN, use any of the following URLs:

So let’s look at what’s new…

Removed Features

Before we look at the new and shiny stuff, you should check your existing code. Are you using:

  • event.layerX or event.layerY
  • jQuery.isNaN() (undocumented utility function)
  • jQuery.event.proxy() (undocumented method)

If you are, jQuery 1.7 may break your system.

New Event APIs: on() and off()

The new on() and off() methods unify event handling in jQuery so there’s no need to use bind(), delegate() or the older live() calls. The syntax:

$(elements).on( events [, selector] [, data] , handler );
$(elements).off( [events] [, selector] [, handler] );

Example 1 — bind a click event to a link:

// onclick
$("a#mylink").on( "click", MyHandler );

// identical 
// to: $("a#mylink").bind( "click", MyHandler );
// or: $("a#mylink").click( MyHandler );

Example 2 — bind a click event to all items in an unordered list using a delegate:

// onclick
$("ul#mylist").on( "click", "li", MyHandler );

// identical to: 
// $("ul#mylist").delegate( "li", "click", MyHandler );

The existing bind() and delegate() methods still exist, but the team recommend you use on() and off() for all new projects using jQuery 1.7.

HTML5 Support for IE6, 7 and 8

Methods such as html() now support HTML5 tags such as header, footer and article. Note that you’ll still require the HTML5 shiv in those browsers.

New isNumeric() method

As you probably guessed, the new isNumeric() function returns true if a value is a number, e.g.

$.isNumeric(10);		// true
$.isNumeric("10");		// true
$.isNumeric(0);			// true
$.isNumeric("");		// false
$.isNumeric(true);		// false
$.isNumeric(Infinity);	// false

New Callbacks() object

The jQuery.Callbacks() object returns a multi-purpose object used to manage callback lists. In essence, it’s a generalized way to queue and trigger a series of handlers. It’s an advanced feature — in most situations, the higher-level jQuery.Deferred object will be preferable.

Miscellaneous Improvements and Bug Fixes

The following issues have been addressed:

  • Delegate event performance has doubled in 1.7. The jQuery team identified that delegation had become increasingly important in application frameworks and made further optimizations to the core code.
  • Toggling animations including slideToggle() and fadeToggle() now work correctly following a termination of queued effects using stop().
  • The is() method now correctly filters positional selectors such as :first, :last and :even against the document — not the jQuery object passed to is().
  • The .removeData() method now accepts one or more keys within an array or a space-separated string.
  • The .stop() method can be passed an optional string representing the name of the animation queue.
  • jQuery now supports the Asynchronous Module Definition (AMD) for defining modules and dependencies.

Documentation Updates

For further details of all the new features, refer to the 1.7 documentation page. All new features are tagged with “New in 1.7”.

Should You Upgrade?

That’s the million dollar question. The jQuery team work hard to minimize compatibility issues and version 1.7 is unlikely to break your code. That said, if you’re particularly risk-averse, delay updating for a week or two. Minor bugs are normally found, fixed and released quickly.