Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery does not execute on $(document).change()

I need to make some JQuery execute when the page/document has changed - in this case, when a div with a specific CSS class is displayed.

I have the following JQuery code:

   <script>
           $(document).change(function () {
               if ($('.validation_errors').length) {
                   alert("test");
               }
           }
    </script>

However, it does not execute and display the alert. Am I missing something here?

like image 782
Theomax Avatar asked Sep 19 '12 12:09

Theomax


1 Answers

Change is only for input, textarea or select elements. Instead you need to bind a function to the DOMSubtreeModified mutation event:

$(document).bind('DOMSubtreeModified', function () {
   if ($('.validation_errors').length) {
       alert("test");
   }
});

EDIT: If your target browsers support it, you should use a MutationObserver instead.

like image 170
noj Avatar answered Oct 06 '22 01:10

noj