FlyingCat
FlyingCat

Reputation: 14250

Assign two functions to the same button?

I am trying to attach a click event to a button to pop more div. I also want the same button to do another functon after more divs are poped and the button is clicked. How do I do that?

Thanks in advance.

my code:

Jquery

$('#add').click(function(){
        $('.detail').fadeIn(300);
        $(this).attr('id', 'add_go');

     })


$('#add_go').click(function(){
     //same button click the second time
     alert('second function!');
})

html

<div>
   <a href='#' id='add'>add</div>

<div class='detail' style='display:none;'>more dive</div>
<div class='detail' style='display:none;'>more dive</div>
<div class='detail' style='display:none;'>more dive</div>

</div>

Upvotes: 1

Views: 184

Answers (1)

Jon P
Jon P

Reputation: 19772

I'd keep it the one function in the situation you've described:

$('#add').click(function(){
      //Check if divs are visible
      if($('.detail').is(':visible'){
         alert('second function');   
      }else{            
         $('.detail').fadeIn(300);
      }
})

Upvotes: 2

Related Questions