Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
176
rated 0 times [  181] [ 5]  / answers: 1 / hits: 104376  / 12 Years ago, wed, may 16, 2012, 12:00:00

I'd like my event to be triggered when a div tag containing a trigger class is changed.




I have no idea how to make it listen to the class' adding event.




<div id=test>test</div>

<script type=text/javascript>
document.getElementById.setAttribute(class, trigger);

function workOnClassAdd() {
alert(I'm triggered);
}
</script>

More From » javascript

 Answers
24

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


[#85554] Monday, May 14, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
yosefleod

Total Points: 113
Total Questions: 100
Total Answers: 115

Location: Egypt
Member since Tue, May 3, 2022
2 Years ago
;