Bug Tracker

Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#11416 closed bug (invalid)

The .on() binding not work on ajax callback which element is newly created, but .live() works

Reported by: leckylao@… Owned by: leckylao@…
Priority: undecided Milestone: None
Component: unfiled Version: 1.7.1
Keywords: Cc:
Blocked by: Blocking:

Description

$('a.ajax_destroy')

.on 'ajax:success', (event, data, status, xhr) =>

console.log 'here'

Sorry for coffee-script. Basically I bind the link's ajax success callback, it works on existing links but not on newly created link.

However It works on using .live(). And the docs http://api.jquery.com/live/ says .live() is deprecated. Therefore, I assume this is a bug.

Change History (7)

comment:1 Changed 7 years ago by anonymous

Browser: Google Chrome 17.0.963.56

comment:2 Changed 7 years ago by sindresorhus

Owner: set to leckylao@…
Status: newpending

Thanks for taking the time to contribute to the jQuery project! Please provide a complete reduced test case on jsFiddle to help us assess your ticket.

Additionally, be sure to test against the jQuery Edge version to ensure the issue still exists. To get you started, use this boilerplate: http://jsfiddle.net/FrKyN/ Open the link and click to "Fork" (in the top menu) to get started.

comment:3 in reply to:  2 Changed 7 years ago by Lecky Lao <leckylao@…>

I am happy to help and wish to provide reduced test case if I can. But as I mentioned. This case involved ajax request and I am not sure how to provide test which involve ajax.

comment:4 Changed 7 years ago by dmethvin

Resolution: invalid
Status: pendingclosed

You're using .on() incorrectly, please re-read the docs or ask for help on the forum.

comment:5 in reply to:  4 Changed 7 years ago by Lecky Lao <leckylao@…>

Replying to dmethvin:

You're using .on() incorrectly, please re-read the docs or ask for help on the forum.

OK, I've re-read the docs and rewrite to use delegated event handler:

$('ul li').on('ajax:success', 'a.ajax_destroy', function(event, data, status, xhr) {

console.log('here');

But it's still not binding the newly created element that got created after the ajax callback. And I think I am using the correct format in this case.

comment:6 Changed 7 years ago by dmethvin

Sorry, my reply was ambiguous.

You're using .on() incorrectly, please re-read the docs AND ask for help on the forum.

This is the bug tracker, so we accept bug reports. It sounds like you need programming help.

comment:7 in reply to:  6 Changed 7 years ago by Lecky Lao <leckylao@…>

Replying to dmethvin:

I have made it work with: $(document).on('ajax:success', 'a.ajax_destroy', function(event, data, status, xhr) instead of $('ul li').on('ajax:success', 'a.ajax_destroy', function(event, data, status, xhr).

But on the doc says it's possible that "A delegated-events approach attaches an event handler to only one element". So is it a bug here or not really?

Note: See TracTickets for help on using tickets.