#11416 closed bug (invalid)
The .on() binding not work on ajax callback which element is newly created, but .live() works
Reported by: | Owned by: | ||
---|---|---|---|
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 11 years ago by
comment:2 follow-up: 3 Changed 11 years ago by
Owner: | set to [email protected]… |
---|---|
Status: | new → pending |
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 Changed 11 years ago by
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 follow-up: 5 Changed 11 years ago by
Resolution: | → invalid |
---|---|
Status: | pending → closed |
You're using .on()
incorrectly, please re-read the docs or ask for help on the forum.
comment:5 Changed 11 years ago by
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 follow-up: 7 Changed 11 years ago by
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 Changed 11 years ago by
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?
Browser: Google Chrome 17.0.963.56