I'm writing a simple script for detecting changes in the DOM, and then performing some actions.
I want to achieve something like this:
$('body').on('DOMNodeInserted', '#entry', function() {
// check if inserted element contains text, then run function
})
You can use the event.target property of the passed in event object within the handler and perform a check using methods such as indexOf()
or similar to determine if the text you are looking for is present.
$(document).on('DOMNodeInserted', function(e) {
var $targetElement = $(e.target);
if($targetElement.text().indexOf('Test') > -1) {
console.log('foo');
}
});
Here is a fiddle demonstrating a simple check of inserted elements' text using text()
and indexOf()
.