3

I need to trigger an event when i add a specific class on button, but I have no idea how to make it listen to the class adding event.

I have something like this:

<script type="text/javascript">   

$(document).ready(function() {
 

    $(id_element).addClass("active");
 
     //this function will fire on add "active" class to "id_button" element
     function fireOnActiveClass() {
        //do something
     }


} 

</script>

I can't touch code $(id_element).addClass("active");, for more reasons.

Currently I use jQuery 1.7.2

Could you please tell me how or which I need to know?

3
  • 1
    Mutation Observer? Commented Dec 5, 2014 at 13:06
  • 1
    There are many different ways to do that but the question would be why you need that regarding your posted code because obviously you already know when you add this specific class Commented Dec 5, 2014 at 13:07
  • If you don't need to support older browsers, you could use animation/transition end event using fake animation/transition and using specific CSS rule. Or extend addClass() method to trigger custom event, or... Commented Dec 5, 2014 at 13:14

5 Answers 5

1

You are modifying the class of the control in your code. So why not simply call the click of the button when you change the class ?

$(id_element).addClass("active");
$(id_button).click();

$(id_button).click(function () {
     if ($(this).hasClass) {
     //do something

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

Comments

1

There is no event raised when a class changes. The alternative is to manually raise an event when you programatically change the class:

$(document).ready(function() {
    $(id_element).addClass("active").trigger('classChange');
    $(id_element).on('classChange', function() {
         // do stuff
    });
});

3 Comments

Why would you need to trigger a custom event here? Just call method once class is added
@A.Wolff Triggering a custom event offers more flexibility and allows one to listen to event bubbling, perhaps? It is also more extensible.
But as you still need to trigger it, I don't see any purpose here (regarding this concrete example) PS: ya regarding flexibility and maybe bubbling
0

One other way would be to call a function right after adding the class like:

$.fn.classAdded = function() {
    var $button = $(this);
    if($button.hasClass('id_button')) {
        // ...
    }
    return $button;
}

$(id_element).addClass("active").classAdded();

Comments

0

I have found solution using this jQuery Plugin: http://meetselva.github.io/attrchange/

Comments

0

Could this jQuery plugin be any help?

https://github.com/tormjens/jquery-dom-router

You can change the default element using $.DOMRouter.defaults.element = '#element'; before initalizing the plugin.

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.