Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There is no event raised when a class changes. The alternative is to manually raise an event when you programatically change the class:</p> <pre><code>$someElement.on('event', function() { $('#myDiv').addClass('submission-ok').trigger('classChange'); }); // in another js file, far, far away $('#myDiv').on('classChange', function() { // do stuff }); </code></pre> <hr> <p><strong>UPDATE</strong></p> <p>This question seems to be gathering some visitors, so here is an update with an approach which can be used without having to modify existing code using the new <code>MutationObserver</code>:</p> <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code"> <pre class="snippet-code-js lang-js prettyprint-override"><code>var $div = $("#foo"); var observer = new MutationObserver(function(mutations) { mutations.forEach(function(mutation) { if (mutation.attributeName === "class") { var attributeValue = $(mutation.target).prop(mutation.attributeName); console.log("Class attribute changed to:", attributeValue); } }); }); observer.observe($div[0], { attributes: true }); $div.addClass('red');</code></pre> <pre class="snippet-code-css lang-css prettyprint-override"><code>.red { color: #C00; }</code></pre> <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt;/script&gt; &lt;div id="foo" class="bar"&gt;#foo.bar&lt;/div&gt;</code></pre> </div> </div> </p> <p>Be aware that the <code>MutationObserver</code> is only available for newer browsers, specifically Chrome 26, FF 14, IE 11, Opera 15 and Safari 6. See <a href="https://developer.mozilla.org/en/docs/Web/API/MutationObserver" rel="noreferrer">MDN</a> for more details. If you need to support legacy browsers then you will need to use the method I outlined in my first example.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload