ElNobDeTfm ElNobDeTfm - 4 years ago 112
jQuery Question

Search text on inserted DOM element

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

What is the simplest way of search text on the inserted DOM node?

Answer Source

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) {

Here is a fiddle demonstrating a simple check of inserted elements' text using text() and indexOf().

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download