3

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

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Gale
  • 406
  • 13
  • 26
  • Is there any way you can post some code to help us better the question fully? For the second question, you can check to see if an element has a class by using the `hasClass()` function (http://api.jquery.com/hasClass/). – Chase Sep 06 '12 at 12:37
  • 1
    According to this answer http://stackoverflow.com/a/1950052/692560 there isn't a built in way of listening to classes being added/removed, but another answer from the same question (http://stackoverflow.com/a/1950199/692560) gives a way of getting a similar effect assuming all changes to classes are done with JQUery. I can't see any DOM event that would be triggered by an element being hidden or shown either, but the same principle of overwriting JQuery functions could be applied. – Vala Sep 06 '12 at 12:38
  • if you control the code that is causing the show/hide, you should just execute your other code there. – jbabey Sep 06 '12 at 12:43
  • @Thor84no: Overrideing the jquery function looks like a solution to my problem – Gale Sep 06 '12 at 12:53

3 Answers3

2

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:

https://developer.mozilla.org/en-US/docs/DOM/DOM_event_reference?redirectlocale=en-US&redirectslug=DOM_Events

http://www.w3.org/TR/DOM-Level-3-Events/#events-MutationEvent

Disclaimer: Tested only in Mozilla Firefox!

techfoobar
  • 65,616
  • 14
  • 114
  • 135
1

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);
});

http://jsfiddle.net/KFGrL/2/

To check if an element has a class name:

if ($('#Element').hasClass('className')) {
    // your code
}
Ghassan Elias
  • 2,213
  • 1
  • 14
  • 17
0

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..

Michal Klouda
  • 14,263
  • 7
  • 53
  • 77