Is there a way in jquery to listen for a change to a node's class and then take some action on it if the class changes to a specific class? Specifically, I'm using the jquery tools tabs plugin with the slideshow and as the slideshow is playing, I need to be able to detect when the focus is on a particular tab/anchor so that I can unhide a specific div.
In my specific example, I need to know when:
<li><a class="nav-video" id="nav-video-video7" href="#video7-video">Video Link 7</a></li>
changes to the following with the class "current" added:
<li><a class="nav-video" id="nav-video-video7 current" href="#video7-video">Video Link 7</a></li>
Then I want to unhide a div at that moment.
Thanks!
I know this is old, but the accepted answer uses
DOMSubtreeModified
, which has now been deprecated for theMutationObserver
. Here's an example using jQuery (test it out here):You can bind the
DOMSubtreeModified
event. I add an example here:http://jsfiddle.net/hnCxK/13/
Here are some other finds that might provide a solution:
And as it was suggested in #2, why not make
current
a class that is added, rather than a ID, and have the following CSS handle the show/hide action.Might also be worth it to look into .on() in jquery.
With anything like this you have essentially two options, callbacks or polling.
Since you possibly can't get an event to be triggered when the DOM mutates in this way (reliably across all platforms) you may have to resort to polling. To be a bit nicer about it, you could try using the requestAnimationFrame api rather than just using something like setInterval natively.
Taking the most basic approach (i.e. using setInterval and assuming jQuery) you might try something like this: