I propose adding support for the "load" and "ready" events to the .live() method.
One of my favorite features of jQuery is .live(), since it allows writing event handlers in a declarative manner. In stead of explicitly attaching an event handler each time a new html element is created, an event handler can be specified once using .live(), with confidence that it will be used by all html elements created in the future. Unfortunately, there are two important events currently missing from .live(): The "load" and "ready" events.
Imagine the following scenario:
- The user is shown a list of email messages that can be "starred", like in gmail.
- The HTML doesn't specify which messages are starred; the stars are added via javascript.
I would like to be able to implement this as follows:
- $('.email-message').live('load', function () {
- if (isMessageStarred(this)) {
- $(this).addClass("has-star");
- } else {
- $(this).addClass("no-star");
- }
- }
This would set the appropriate "has-star"/"no-star" class for each .email-message element, as soon as it is created. With jQuery 1.4, this is unfortunately not possible. In stead, one would have to explicitly set the class of .email-message elements each time a new message is loaded.
This type of scenario is quite common, so support for the "load" and "ready" events in .live() would be a useful and important addition to jQuery.