Is it possible in javascript / jQuery to bind a function to a DOM element’s classname changing or being assigned a new class?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
No, there’s no broadly-supported event fired when the class is updated. You’ll have to poll. Some browsers support DOM mutation events, but it’s never been completely supported cross-browser, see the warning on the linked page.
Obviously, if it’s your code that changes the class, you could create a function you use to do that and always use that function rather than changing it directly, but if you’re looking for changes to it outside of your code, I don’t see an alternative to polling.
If you poll, be sure to test on all of your target browsers and do as little work actually in the polling code as you can. It’s easy to over-do it (slowing down the page).