Search code examples
jqueryloadcode-duplication

Jquery Code on more than one function


The following code I have load on when a link is clicked:

$('.ops .menu .panel a').on('click',function(){
                        $('.ops .lay').css('opacity',0.1).load($(this).attr('href'), function(){ $('.ops .lay').css('opacity',1) });
                        $(this).parent().parent().find('a').removeClass('active'); $(this).addClass('active');
                        return false
                    });

I want this code to also be insde another load function. Do I have to repeat it or is there another way. Can it be stored somewhere?


Solution

  • function act(){
         $('.ops .lay').css('opacity',0.1).load($(this).attr('href'), function(){ $('.ops .lay').css('opacity',1) });
         $(this).parent().parent().find('a').removeClass('active'); $(this).addClass('active');
         return false
    };
    
    $('.ops .menu .panel a').on('click', act);
    

    use act method somewhere else too :)