Based on this question, I wrote the following code:
$(function() {
$( document ).on('DOMNodeInserted',"span:contains('Suggested Post')", function() {
console.log("New Suggested Post found");
})
})
As I scroll down the page and these elements are created, I do not see the console message I expect to see. However, if I manually input $("span:contains('Suggested Post')").length
in the console, the output continues to increase.
Ideas?
Snippet requested by @Barmar. It doesn't behave exactly like my real-world scenario, but it's still not right...
$(document).on('DOMNodeInserted', "div:contains('Banana')", function() {
console.log("new banana detected!");
});
$('#btn').on('click', function() {
var fruit = '';
switch (Math.floor((Math.random() * 3) + 1)) {
case 1:
fruit = 'Apple'
break;
case 2:
fruit = 'Orange'
break;
case 3:
fruit = 'Banana'
break;
default:
fruit = "WTF"
}
$('#fakeDocument').append('<div>' + fruit + '</div>');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="fakeDocument">
<strong>Banana Detector</strong>
<button id="btn" type="button">Add Banana!</button>
</div>