Search code examples
javascriptperformanceoptimizationcode-snippets

Optimize J$ plus combine 3 <scripts> into 1


May you please tell me how you would optimize this J$ and merge it as well. Basically, the they are duplicates of each other but just add different classes to different elements.

You can see the "drawer" on this website - look for the "Get Your FREE..."

<script>
$(document).ready(function(){
    $("#drawer-closed").click(function(){
        $(".get-free-program .btn").addClass("expander425");
    });
});
</script>

<script>
$(document).ready(function(){
    $("#drawer-closed").click(function(){
        $(".get-free-program form").addClass("showform");
    });
});
</script>

<script>
$(document).ready(function(){
    $("#drawer-closed").click(function(){
        $(".get-free-program img.arrowToggler").addClass("arrow-flipper");
    });
});
</script>

Solution

  • <script>
    $(document).ready(function(){
        $("#drawer-closed").click(function(){
            $(".get-free-program .btn").addClass("expander425");
              $(".get-free-program form").addClass("showform");
     $(".get-free-program img.arrowToggler").addClass("arrow-flipper");
      });
    });
    </script>
    

    Should do