I know that I can fire functions after ajax call using the success option, e.g. to manipulate loaded content, like
$.ajax({
url : url,
...
success: function(data, textStatus, jqXHR){
alert($('#overlay > #popup input').length);
},
...
});
I also know that I can apply interactive events like click on ajax loaded content via
$(document).on('click','#overlay > #popup',function(e) {
alert($(e.target).find('input').length);
});
which works ofcourse.
I try to find a way, do the same alert automatically, only after content has been loaded, instead of click on it, like this (sorry for this bad NON-working example)
$(document).on('load','#overlay > #popup',function(e) {
alert($(e.target).find('input').length);
});
EXAMPLE for explaination, not part of the question
At the end I want to apply calculated styles to loaded input[type=radio] using jQuery (individual width, depending on its value attribute, to be displayed automatically in css pseudo element ::after {content: attr(value);}
)
The current html output from php function is
<label class="fl" style="width: 80px;">
<input type="radio" name="' . $name . '" value="' . $year . '" ' . ($year == date('Y')?'checked="checked"':'') . '/>
</label>
the current result looks like this
where the visible captions are the ::after elements. Only I want – in case of different value length – the width is set automatically, not hard coded.
Can somebody open this node in my head ?
2
Answers
SOLUTION based on @CertainPerformance 's answer
Works exactly as I want. And the entire thing saves ajax payload, as intended.
An here's the result (with nonsense content, for demo only)
If you know which element is going to be appended to on ajax complete, you can attach a MutationObserver to it. When the observer fires, check to see if the element you’re curious about exists as a descendant:
But while this is possible, it’s pretty bad practice. If you know when an element is going to be created, it’d be much better to put all the functionality for that inside the ajax success callback, rather than use MutationObserver (which should generally be reserved only for situations when other options are impossible, like when you don’t have control over the code).
For elements loaded after a network request, you can also use ajaxComplete with jQuery: