{"action":"created","issue":{"url":"https://api.github.com/repos/AllThingsSmitty/jquery-tips-everyone-should-know/issues/33","labels_url":"https://api.github.com/repos/AllThingsSmitty/jquery-tips-everyone-should-know/issues/33/labels{/name}","comments_url":"https://api.github.com/repos/AllThingsSmitty/jquery-tips-everyone-should-know/issues/33/comments","events_url":"https://api.github.com/repos/AllThingsSmitty/jquery-tips-everyone-should-know/issues/33/events","html_url":"https://github.com/AllThingsSmitty/jquery-tips-everyone-should-know/issues/33","id":113919488,"number":33,"title":"Re-attach events when the DOM reloads. EG: on ajax pagination","user":{"login":"leroy0211","id":2051658,"avatar_url":"https://avatars.githubusercontent.com/u/2051658?v=3","gravatar_id":"","url":"https://api.github.com/users/leroy0211","html_url":"https://github.com/leroy0211","followers_url":"https://api.github.com/users/leroy0211/followers","following_url":"https://api.github.com/users/leroy0211/following{/other_user}","gists_url":"https://api.github.com/users/leroy0211/gists{/gist_id}","starred_url":"https://api.github.com/users/leroy0211/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/leroy0211/subscriptions","organizations_url":"https://api.github.com/users/leroy0211/orgs","repos_url":"https://api.github.com/users/leroy0211/repos","events_url":"https://api.github.com/users/leroy0211/events{/privacy}","received_events_url":"https://api.github.com/users/leroy0211/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-10-28T20:52:29Z","updated_at":"2015-11-09T10:29:24Z","closed_at":null,"body":"When you load content through ajax (or pjax), and events are registered on elements inside that content. The events registered are not handled anymore. Because the previous DOM elements are 'destroyed'. The events should be re-attached on the DOM elements. A best practice is to attach the events on a global DOM element. \r\nFor Example: \r\n\r\n```html\r\n<div id=\"content\">\r\n   <!-- this content will be changed with ajax //-->\r\n</div>\r\n```\r\nInstead of attaching the events on the elements within the content, attach the events on the DOM element which will NOT be loaded or changed through ajax (in this case: `#content`, or sometimes: `body`).\r\n\r\n```javascript\r\n$('#content').on('click', 'a', function(e){\r\n    // here the logic when clicking on a link inside the `#content` div\r\n});\r\n``` "},"comment":{"url":"https://api.github.com/repos/AllThingsSmitty/jquery-tips-everyone-should-know/issues/comments/155023308","html_url":"https://github.com/AllThingsSmitty/jquery-tips-everyone-should-know/issues/33#issuecomment-155023308","issue_url":"https://api.github.com/repos/AllThingsSmitty/jquery-tips-everyone-should-know/issues/33","id":155023308,"user":{"login":"mynamesleon","id":4477232,"avatar_url":"https://avatars.githubusercontent.com/u/4477232?v=3","gravatar_id":"","url":"https://api.github.com/users/mynamesleon","html_url":"https://github.com/mynamesleon","followers_url":"https://api.github.com/users/mynamesleon/followers","following_url":"https://api.github.com/users/mynamesleon/following{/other_user}","gists_url":"https://api.github.com/users/mynamesleon/gists{/gist_id}","starred_url":"https://api.github.com/users/mynamesleon/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mynamesleon/subscriptions","organizations_url":"https://api.github.com/users/mynamesleon/orgs","repos_url":"https://api.github.com/users/mynamesleon/repos","events_url":"https://api.github.com/users/mynamesleon/events{/privacy}","received_events_url":"https://api.github.com/users/mynamesleon/received_events","type":"User","site_admin":false},"created_at":"2015-11-09T10:29:24Z","updated_at":"2015-11-09T10:29:24Z","body":"I wouldn't necessarily say that the use of delegated events is a best practice - it very much depends on the situation. If it's a parent element that isn't too far up the DOM tree, then great. It gives you the advantage of not having to rebind events in ajax callbacks, and saves memory because you bind the event on one element rather than several.\r\n\r\nBut, a lot of people use this as a lazy solution by binding the event to the document. Relying on detecting events as they bubble up the DOM tree can cause a significant performance impact (delegated events bound to the document is what the jQuery .`live` function used to do, but the performance impact is why it was removed from the code base). Also, relying on these events bubbling up to the document level doesn't working on iOS. It only allows events on certain elements (namely anchors and buttons) to bubble up that far for performance reasons.\r\n\r\nSo this is a good point to make, but the performance impact needs to be considered."}}