3

Is there any way (aside from creating a new event) that I can tell when a particular CSS class has been added to an element?

0

3 Answers 3

4

If you don't have access to the code that's adding the class, there's a jQuery plugin called livequery that will allow you to run code when elements are added to the DOM (or when you do things like add a class to an element).

http://brandonaaron.net/code/livequery/docs

$('div.myClass').livequery(function() {
     alert('myClass was added');
});

$('#someDiv').addClass("myClass");  // The livequery code will run

This works specifically with jQuery methods.

Sign up to request clarification or add additional context in comments.

Comments

0

You could extend jQuery's addClass() to fire an event when it adds a class. However, this means you'll have to add classes always with this method.

If you don't do that, you'll have to poll and look for differences in the class attribute. I don't recommend doing that. Besides performance, you'll need to handle classes being removed too.

Comments

0

There is a DOM Level 3 specification to detect changes to an elements attributes, it is supported in a couple of browsers... Also IE supports an onpropertychange (http://msdn.microsoft.com/en-us/library/ms536956(VS.85).aspx) event too.

It's probably not going to be enough though. Your best bet is use window.setInterval() and check if the value has changed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.