Skip to content

jQuery.on() for document fragment (off-DOM) already supported? #548

@xorcus

Description

@xorcus

Documentation for .on() method states in bold: "Event handlers are bound only to the currently selected elements; they _must exist on the page_ at the time your code makes the call to .on()."

Is this requirement for jQuery elements to be in DOM obsolete?

It seems that .on() works just fine for registering event handlers on document fragments created using jQuery, even before they are added into DOM. Of course, event won't be fired, but the call to .on() may take place before adding elements to DOM.

http://james.padolsey.com/stuff/jQueryBookThing/
The second half of the article also mentions this: "Among other things, this means that you can safely bind event handlers during element creation, and they will be added using jQuery's robust event mechanism. "

If this can be adopted as official jQuery support, could you please update the API docs?

[edit]
The same paragraph in .on() API docs ends with: "If _new HTML_ is being injected into the page, select the elements and _attach event handlers after the new HTML is placed into the page_. Or, use delegated events to attach an event handler, as described next."
So this also implies that content must be in-DOM

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions