3

Hi this is my html structure.

  <div class="test-class" style="background-color: red;" >1</div>
  <div class="test-class" style="background-color: blue;" >1</div>
  <div class="test-class" style="background-color: green;" >1</div>

Here on first page load the background-color of test-class come randomly .

i want to run an function when the background color of test-class is changed . How to do this ?

Edit

so from Is it possible to listen to a "style change" event? this question i got a solution

(function() {
    var ev = new $.Event('style'),
        orig = $.fn.css;
    $.fn.css = function() {
        $(this).trigger(ev);
        return orig.apply(this, arguments);
    }
})();

$('.test-class').bind('style', function(e) {
    console.log( $(this).attr('style') );
});

But please help to apply this answer . i want to alert the content of that div and it's color when it's background-color is changed .

Community
  • 1
  • 1
Jafar tm
  • 247
  • 2
  • 11
  • 3
    Possible duplicate of [Is it possible to listen to a "style change" event?](http://stackoverflow.com/questions/2157963/is-it-possible-to-listen-to-a-style-change-event) – David R Jan 17 '17 at 06:13

1 Answers1

1

If some javascript changes the background color, can you just call the function after changing the background?

seport
  • 35
  • 9
  • Yeah, that would be the best way to do it. Silly to do it any other way. You'd have to store the original value in a variable and compare it to the possibly new, current value at set intervals... not worth the headache. – Gary Hayes Jan 17 '17 at 07:02
  • what happen if we can't detect which event make change in background colour ? or if the external jquery file make change in div's background .? – Jafar tm Jan 17 '17 at 07:21