Vishnu Suresh Vishnu Suresh - 5 months ago 20
jQuery Question

can multiple click events be used on the same element?

$("#button1").click(function(e)
{
//action
});
$("#button2").click(function(e)
{
//do something
$("#button1").click(function(f)
{
//do something else
});
});


I have two buttons doing different actions.but if button 2 is clicked,i need button 1 to do a different task on the next click without the first function being executed.
any suggestions?

Answer

For that ,you need to use one variable scope for detect whether button 1 or 2 is click

var btn = 1; // default
$("#button1").click(function(e)
{
  if(btn){
    #button1 click
  }
  else{
    #after button2 click
  }
});
$("#button2").click(function(e)
{
    btn = 2; //change value after button2 click 
});