tags:

views:

19

answers:

1

I have a slider with 4 elements whose any of them has the class 'control_tab'. The active element has the class 'active'. A few seconds later, the slider moves along and the next element becomes the active one. Then it has the class 'active'; the previous element that has been previously active looses the class 'active'.

I need to know when an element becomes the active one. As soon as any element becomes the active one by being added the class 'active', the active element's index is printed out to the console.

Here is the sample code:

<div id="controls">   
  <a href="#" class="control_tab">
    <span>A</span>
  </a>
  <a href="#" class="control_tab active">
    <span>B</span>
  </a>
  <a href="#" class="control_tab">
    <span>C</span>     
  </a>
  <a href="#" class="control_tab last">
    <span>D</span>    
  </a>    
</div>

Sorry if the message is a little bit confused,I am exhausted to ponder that stuff :p

Thanks for your help.

R.

+1  A: 

If you don't have control over the code that is adding/removing the active class, there's a plugin called livequery that will fire code for you when there's a DOM change that matches a selector.

http://brandonaaron.net/code/livequery/docs

$('.active').livequery(function() {
    console.log($(this).index() + ' is now active');
});

It can also fire code when an element is removed (or unmatched).

$('.active').livequery(function() {
    console.log($(this).index() + ' is now active');
}, function() {
    console.log($(this).index() + ' is no longer active');
});
patrick dw
@Patrick: Thanks a lot, the livequery plugin is EXACTLY what I was looking for. I thought that the live query was replaced in concept by the delegate in the latest version 1.4.2. Never will I be able to live without using that plugin :p@pointy/@Patrick: The slider plugin is called "cycle" and is part of a Wordpress theme I purchased a couple of days ago. It is full of great visual effects with tons of jQuery effects/plugins underneath. Editing a few lines of code in the original script would have been a nightmare to maintain.Once again, thanks a lot for your help, you rock! ;)
roland