Skip to content Skip to sidebar Skip to footer

How To Write A Reusable Jquery Click Function?

I have this accordion function : $('.accordion a').click(function () { if($(this).is('.accordionClose')) { $('.accordionOpen').toggleClass('accordionOpen').to

Solution 1:

Write like this:

functionfunc_name(){
//do stuff here
}

//call this later like this:

$('.accordion a').click(func_name);

//Or call like this:
$('.accordion a').click(function(){
  func_name(); //parenthesis required here
});

Also, you may look at another post for stepping up a little more.

Solution 2:

If you want to do exactly the same thing on some other click then You can also trigger the same click event by trigger like :

$(".xyz_class").click(function(){
   $(".accordion a").trigger("click");
});

Docs are here. Hope that helps.

Post a Comment for "How To Write A Reusable Jquery Click Function?"