2

I am currently using Javascript like so:

$(button).css("background-color", color1);
$(button).css("background-color", color2);

I would like to add event handlers to the button element, so that I would be able to keep track of times that the button colours were changed, that is every time the button is set to color1, the time is kept, and also the time when the button is set to color2. Does anyone know how to do this please, because I can't seem to find the right event handler for this?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Questionnaire
  • 656
  • 2
  • 11
  • 25
  • Possible duplicate of [Is it possible to listen to a "style change" event?](https://stackoverflow.com/questions/2157963/is-it-possible-to-listen-to-a-style-change-event) – Karen Grigoryan Feb 20 '18 at 20:04

1 Answers1

0

Instead of passing the color value as string you could create a function that returns a string,

the function you use can perform other opertations, such as populating a list of timestamps, before returning:

var store = []

$(button).css("background-color", function(){
   store.push(Date.now())
   return color1
});
maioman
  • 18,154
  • 4
  • 36
  • 42