I need to call a function just after some elements are loaded on the page(result of a ajax call) after the page loads, I cannot use the success callback function. Is there a way to do this?
Asked
Active
Viewed 457 times
0
-
1I don't understand what you want. Your question is very different form the title of the question. – slebetman Mar 19 '15 at 07:27
-
1Use [MutationObserver](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver) to detect moment when elements are added into DOM, then execute code in listener callback. – Klaster_1 Нет войне Mar 19 '15 at 07:29
1 Answers
0
From your title seems that you need to attach a callback (example on click callback). You can use event delegation directly with javascript: Example:
// Get the element, add a click listener...
document.getElementById("ajax-target").addEventListener("click",
function(e) {
// e.target is the clicked element!
// If it was a list item
//Future elements
if(e.target && e.target.nodeName == "LI") {
// List item found! Output the ID!
console.log("List item ", e.target.id.replace("post-"), " was clicked!");
}
});
With jQuery this would be really easy:
$('#ajax-target').on('click', '.future-elements', function(e){
console.log('some future elements was clicked');
});

albanx
- 6,193
- 9
- 67
- 97