javascript - Jquery event for changing an element's class attribute -
i need execute jquery code when element's class attribute changed. example, element : <div class="my-class">
. so, when new class added div, (i.e. <div class="my-class new-class">
, have trigger jquery code.
how do in jquery? there event attribute change?
if have absolutely no means of telling when external function changes class (like, custom event, example), have this:
$(document).ready(function(){ // cache required element window.elementinquestion = $('#element-that-changes-class'); function checkclass(){ if (window.elementinquestion.hasclass('new-class')) { // trigger custom event $(document).trigger('elementhasclass'); // if need check until first time class appears // can cancel further checks here clearinterval(window.classcheckerinterval); } } // check if element has class every once in while window.classcheckerinterval = setinterval(checkclass, 100); $(document).bind('elementhasclass', function(){ // handler here }); });
Comments
Post a Comment