Note
I think I can explain this without a jsfiddle. I'll avoid to show the whole code because there is too much HTML with lots of classes that I would have to adapt to the example.
Also, excuse my bad English. I hope you understand me. Im learning.
The point is
I have some modules that overlays my interface when the user activates the respective button. I want all the modules do the same animation when opening.
How is it now?
Now I am repeating again and again the same code (below), but pointing to each module and its respective button... I'm a newb in terms of javascript but I know this is anti-performance... I feel like I can do it cleaner.
My doubt
Is there a way to make a function or something with this code to allow it to work for all the modules I want, without repeating it again and again?
$('#btn-moduleX, #close-moduleX').on('click', function(){
$( "#btn-moduleX" ).toggleClass('layout-color-subBase');
$( "#overlay-moduleX" ).toggleClass('overlay--hidden');
var currentOpacity = $('.fx-appear-soft').css('opacity');
$( ".fx-appear-soft" ).animate({
'opacity': 1 - currentOpacity
}, 300);
});
<button class="someclass">and attach a click to all of these buttons at the same time :$("button.someclass").click(...Then inside use$(this).toggleClass(...<button class="someclass" data-module="module1">. Then you access the data usingmoduleName = $(this).data('module').