JQuery not executing in $ (document) .change ()

I need to execute jQuery when a page / document changes - in this case, when a div is displayed with a specific CSS class.

I have the following jQuery code:

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

However, it does not execute and does not display a warning. Did I miss something?

+6
source share
3 answers

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

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

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

+17
source

You cannot control changes in the DOM that way. Quote from jQuery docs:

A change event is dispatched to an element when its value changes. This event is limited to items, boxes, and items. - http://api.jquery.com/change/

If you want to execute code when the DOM changes, you may need to either configure it or the interval that the DOM checks for or uses the technique provided by the poster on jQuery forums.

There is a DOMSubtreeModified event, but it is not widely available in current browsers, so I personally do not recommend using it. See fooobar.com/questions/19781 / ... for more information on this.

+2
source

Your syntax is wrong, too buddy! If you are going to do this, make it ready in the document, the exchange is very limited and will not start on a new page.

 $(document).ready(function(){ if($('.classOrId').length){ //do something if it exists }; }); 
0
source

Source: https://habr.com/ru/post/925765/


All Articles