Detecting when the li element class change

Detecting when the li element class change

Hi there,

I am posting in order to find a solution to this but i can't find anything,
I am trying to change to change the background of a li element when its own class is changing
i tried :
  1.  $("ul#slide-nav li").each(function() {
                    if ( $(this).hasClass("selected") ){
                    $(this).css("background-color","black");
                   }else{
                       $(this).css("background-color","gray");
              
               }              
       });









This seems to be working i think., but once the class change again it doesn't work.. how can it loop for ever ?

Thank you in advance.