Lompat ke konten Lompat ke sidebar Lompat ke footer

Jquery Class Change Event !!


javascript - How to fire an event on class change using jQuery? - Stack

There is no event raised when a class changes. The alternative is to manually raise an event when you programatically change the class: $someElement.on ('event', function () $ ('#myDiv').addClass ('submission-ok').trigger ('classChange'); ); // in another js file, far, far away $ ('#myDiv').on ('classChange', function () // do stuff ); There is no event raised when a class changes. The alternative is to manually raise an event when you programatically change the class:

This question seems to be gathering some visitors, so here is an update with an approach which can be used without having to modify existing code using the new MutationObserver:

Be aware that the MutationObserver is only available for newer browsers, specifically Chrome 26, FF 14, IE 11, Opera 15 and Safari 6. See MDN for more details. If you need to support legacy browsers then you will need to use the method I outlined in my first example.

You could replace the original jQuery addClass and removeClass functions with your own that would call the original functions and then trigger a custom event. (Using a self-invoking anonymous function to contain the original function reference)

This approach does make the assumption that the classes will only be changed via the jQuery addClass and removeClass methods. If classes are modified in other ways (such as direct manipulation of the class attribute through the DOM element) use of something like MutationObservers as explained in the accepted answer here would be necessary.

With these replacement functions you can then handle any class changed via classChanged or specific classes being added or removed by checking the argument to the callback function:

site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.7.16.39771

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.


jQuery change event

Firing events on CSS class changes in jQuery - Stack Overflow

13 Answers13. Whenever you change a class in your script, you could use a trigger to raise your own event. $ (this).addClass ('someClass'); $ (mySelector).trigger ('cssClassChanged') . $ (otherSelector).bind ('cssClassChanged', data, function () do stuff ); How can I fire an event if a CSS class is added or changed using jQuery?Does changing of a CSS class fire the jQuery change() event?

but otherwise, no, there's no baked-in way to fire an event when a class changes. change() only fires after focus leaves an input whose input has been altered.

If you want to detect class change, best way is to use Mutation Observers, which gives you complete control over any attribute change. However you need to define listener yourself, and append it to element you are listening. Good thing is that you don't need to trigger anything manually once listener is appended.

In this example event listener is appended to every element, but you don't want that in most cases (save memory). Append this "attrchange" listener to element you want observe.

The usage is quite simple, just add a new listender on the node you want to observe and manipulate the classes as usually:

change() does not fire when a CSS class is added or removed or the definition changes. It fires in circumstances like when a select box value is selected or unselected.

I'm not sure if you mean if the CSS class definition is changed (which can be done programmatically but is tedious and not generally recommended) or if a class is added or removed to an element. There is no way to reliably capture this happening in either case.

You could of course create your own event for this but this can only be described as advisory. It won't capture code that isn't yours doing it.

Alternatively you could override/replace the addClass() (etc) methods in jQuery but this won't capture when it's done via vanilla Javascript (although I guess you could replace those methods too).

The watch plug-in works by hooking up to DOMAttrModified in FireFox, to onPropertyChanged in Internet Explorer, or by using a timer with setInterval to handle the detection of changes for other browsers. Unfortunately WebKit doesn’t support DOMAttrModified consistently at the moment so Safari and Chrome currently have to use the slower setInterval mechanism.

Good question. I'm using the Bootstrap Dropdown Menu, and needed to execute an event when a Bootstrap Dropdown was hidden. When the dropdown is opened, the containing div with a class name of "button-group" adds a class of "open"; and the button itself has an "aria-expanded" attribute set to true. When the dropdown is closed, that class of "open" is removed from the containing div, and aria-expanded is switched from true to false.

Now I realize this is more specific than the general question that's being asked. But I imagine other developers trying to detect an open or closed event with a Bootstrap Dropdown will find this helpful. Like me, they may initially go down the path of simply trying to detect an element class change (which apparently isn't so simple). Thanks.

If you need to trigger a specific event you can override the method addClass() to fire a custom event called 'classadded'.

if you know a what event changed the class in the first place you may use a slight delay on the same event and the check the for the class.example

site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.7.16.39771

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.


javascript - JQuery Detect class changes - Stack Overflow

There is no event of class-added, you will need to track it yourself It can be done with an infinite loop with setTimeout to check if the class has changed. function checkForChanges() if ($('.slide-out-div').hasClass('open')) $('.otherDiv').css('top','0px'); else setTimeout(checkForChanges, 500); The question's a bit old, but since I came across while looking for a similar problem, thought I'd share the solution I went with here - Mutation Observers

The function in mutation observer is called any time an attribute of .slide-out-div is changed, so need to verify the actual change before acting.

You can use attrchange jQuery plugin. The main function of the plugin is to bind a listener function on attribute change of HTML elements.

site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.7.16.39771

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.


Jquery change event, as of jquery 1

javascript - Event trigger on a class change - Stack Overflow

I'd like my event to be triggered when a div tag containing a trigger class is changed. to the class' adding event. events on CSS class changes in jQuery. 2. Well there were mutation events, but they were deprecated and the future there will be Mutation Observers, but they will not be fully supported for a long time. So what can you do in the mean time?

site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.7.16.39771

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.


.change() | jQuery API Documentation

The change event is sent to an element when its value changes. This event is limited to elements,