I have an element which i know isn't going to have a class on it at page load, but i know it will at some point. Now what are my options of keeping track of the classes added and removed from the element, by other methods using addClass() and removeClass()? I'm using jQuery but would be happy with a vanilla JavaScript solution.
Asked
Active
Viewed 75 times
-1
-
1Possible Duplicate of http://stackoverflow.com/questions/1950038/jquery-fire-event-if-css-class-changed? – Nick Bork Feb 06 '12 at 21:06
-
nice i didnt know you could put a trigger on the class change its awesome ;) – Lil'Monkey Feb 06 '12 at 21:07
-
You have to raise the event yourself when you change css classes OR get the JS that the thread discusses and see if it will work. You could do it by altering the jQuery library (.addClass, .removeClass, .css etc) too. – Nick Bork Feb 06 '12 at 21:10
-
but you should NEVER alter jquery :) – Lil'Monkey Feb 06 '12 at 21:11
2 Answers
-1
in vanilla you can use
document.getElementById('divId').className;
or in jquery
$('#divId').attr('class');

Will P.
- 8,437
- 3
- 36
- 45
-
1I don't think you understand my question. I need to keep track of the classes on an element, they could be added at any point from any piece of code. – benhowdle89 Feb 06 '12 at 20:58
-
Not sure but are you saying that you want to keep an array of all the classes an element has had? – Will P. Feb 06 '12 at 21:07