How to Fire an Event on Class Change Using Jquery

How to fire an event on class change using jQuery?

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

UPDATE - 2015

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:

var $div = $("#foo");
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
var attributeValue = $(mutation.target).prop(mutation.attributeName);
console.log("Class attribute changed to:", attributeValue);
});
});

observer.observe($div[0], {
attributes: true,
attributeFilter: ['class']
});

$div.addClass('red');
.red {
color: #C00;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<div id="foo" class="bar">#foo.bar</div>

Firing events on CSS class changes in jQuery

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

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.

$(function() {  var button = $('.clickme')      , box = $('.box')  ;    button.on('click', function() {     box.removeClass('box');    $(document).trigger('buttonClick');  });              $(document).on('buttonClick', function() {    box.text('Clicked!');  });});
.box { background-color: red; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="box">Hi</div><button class="clickme">Click me</button>

Event trigger on a class change

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?

You can use a timer to check the element.

function addClassNameListener(elemId, callback) {
var elem = document.getElementById(elemId);
var lastClassName = elem.className;
window.setInterval( function() {
var className = elem.className;
if (className !== lastClassName) {
callback();
lastClassName = className;
}
},10);
}

Running example: jsFiddle

How to fire change event in jQuery

For execute an event you can use trigger("eventname") function with JQuery.

Example

$("#id").keypress(function(){  console.log("input keypress");});
$("#btn").click(function(){ $("#id").trigger("keypress");});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><input type="text" id="id"> <button id="btn">Trigger keypress event</button>

jQuery fire function when element specific class change

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



Related Topics



Leave a reply



Submit