Possible Duplicate:
jquery event handler: div becomes visible/hidden
Can I set an event to trigger after some element is hidden and then is set to visible by js?
And also can I detect if js has added class to element?
I'm working with jQuery
Possible Duplicate:
jquery event handler: div becomes visible/hidden
Can I set an event to trigger after some element is hidden and then is set to visible by js?
And also can I detect if js has added class to element?
I'm working with jQuery
Yes. You can track addition and removal of classes via events.
You can track this using the DOMAttrModified
event - which, as the name suggests, is fired every time any attribute of any element in the DOM is modified.
In this particular case we track the modification of the class
attribute for the element we wish to track.
Demo: http://jsfiddle.net/techfoobar/dnRUu/2/
In the demo, click the 'Toggle Class' button to add a class to the first DIV. The alert you get is fired by the DOMAttrModified
event.
More Info Here:
http://www.w3.org/TR/DOM-Level-3-Events/#events-MutationEvent
Disclaimer: Tested only in Mozilla Firefox!
Maybe you can use interval to handle such event:
function checkIt () {
if ($('#Element').length > 0) {
if($('#Element').is(':hidden') == true) {
// add class name
alert('Element was hidden');
$('#Element').removeClass('hidden').addClass('visible');
// or toggle class name
//$('#Element').toggleClass('className');
// or remove class name
//$('#Element').removeClass('className');
// if you want to clear the interval
//clearInterval(listenerID);
}
}
}
var listenerID;
$(function(){
listenerID = setInterval(checkIt, 500);
});
To check if an element has a class name:
if ($('#Element').hasClass('className')) {
// your code
}
There is no such event you could subscribe to. The best you can do is run periodically your own function to check for changes you care about..