tags:

views:

35

answers:

3

Hi guys,

 $(function(){
   $(".first").click(function(){ 
               $(".first ul li").slideUp();
               $(this).find("li").slideDown();        
   });
});

How can I make the active .first element temporary unclickable until its class siblings are referred as active and of course executing the code above is necessary.

Also no matter what type it is: div, li.

Appreciate your help.

+1  A: 

Update

Bad case of Monday sloppy code. Thanks to Peter Ajtai for picking up on that (see comments).

 $(function(){
   $(".first").click(function(event){


               if ($(this).siblings('.active').length !== $(this).siblings().length) {
                   return false;
               }

               $(".first ul li").slideUp();
               $(this).find("li").slideDown();        
   });
});

So if all the siblings don't have an active class, then your function will bail out early.

alex
Why not just `if( $(this).siblings(".active").length !== siblings.length)`?
Peter Ajtai
I guess I should append this "active" class to the element, right?And that "function(event)" thing makes the function Boolean type or?
@Peter Ajtai Of course, see edit :)
alex
A: 

By active, do you mean they have a class called active?

If so, try this:

$(function(){
    $(".first").click(function() {
        if( $( this ).siblings( ':not(.active)' ).length === 0 ) {
             $(".first ul li").slideUp();
             $(this).find("li").slideDown(); 
        }       
    });
});

When the element with the .first class is clicked, it simply gets its .siblings() that do :not() have the active class, and checks the quantity it found using the length property.

When the length is 0, they all have the active class.

patrick dw
No, by active I mean that they are clicked but anyway appending class should work either!
@user - Yes, there should be some way to indicate that they have been clicked. Adding an `active` class is an easy way to do it, plus you can use that class to apply some style in order to give the user a visual cue that it has been clicked (if necessary).
patrick dw
A: 

You can use the .unbind() and then .bind() the event again. But I think the class toggling is more efficient.

Marcio Almada