JQuery one click event for multiple element events

jquery

Solution

You could do this:

$(".class1, .class2").click(function () {
  $(".classer"+$(this).attr('class').substr(-1)).slideToggle("slow");
});

as long as the elements you're clicking on only have one class. This pulls the integer off the element being clicked on and appends it to `.classer`. So clicking on `.class2` would trigger the toggle on `.classer2`.

Problem

My question is related to this one: jQuery same click event for multiple elements However, when I do: ``` $('.class1, .class2').click(function() { some_function(); }); ``` I always use the same function with the same object. What if I wanted to consolidate the following two statements for instance: ``` $(".class1").click(function () { $(".classer1").slideToggle("slow"); }); $(".class2").click(function () { $(".classer2").slideToggle("slow"); }); ``` They reference a different object for each - can I even consolidate this? Thanks, Oliver

Original source

Related problems