views:

71

answers:

1

Hi, I have a button <a> that should toggle a drop-down Then it should also bind (once) a click event to the document that when clicked it should slide it up.

I started with this HTML and JS... Any suggestions how to make this work?

HTML

      <a class="a" href="#">continue shopping</a>
      <div class="b">
        <a href="#">continue 1</a>
        <a href="#">continue 2</a>
        <a href="#">continue 3</a>
        <a href="#">continue 4</a>
      </div>

JS

 $(".a").toggle(function(event){
   buttonEvent = $(event.target)
    $(this).addClass("open").next(".a").slideDown(500);

    $(document).one("click",function(e){
    if(!$(e.target).is(".a") && !buttonEvent.hasClass("b")){
        $(".b").slideUp(500)
     }  
  })

 },
 function(){
  $(this).removeClass("open").next(".continueShopCntnr").slideUp(500)
});

But it is still buggy.. when clicked on again on the continue shopping it doesn't do nothing

+1  A: 

Maybe this would work better... it closes any open ones on the document click.

EDIT: Actually tested this code (vs the previous answer) and it works as I understand the requirements.

$(".continueShop").toggle(
    function(){
      $(this).removeClass("open").next(".continueShopCntnr").slideUp(500)
    },
    function(){
        $(this).addClass("open").next(".continueShopCntnr").slideDown(500);

        $(document).one("click",function() {
           $(".continueShop.open").each(function() { 
               $(this).click();
           });
         });
     }
);
Lindsay
when clicking on the continueShop once it slidesUp and slidesDown
adardesign
I revised the code above. See if it works for you. It's working for me in my little test app. I also switched the toggle function since when the page loads the dialog is already open (at least from the html sample you included) so the first click would close it instead of open it. And I removed the return false because looking at the jQuery docs for toggle it says it already does preventDefault() for you.
Lindsay
Thanks, This should work
adardesign