Same code run for document ready and multiple event

Same code run for document ready and multiple event

I have a code which will run when document ready and some event occurs. In this case I use same code multiple time. But I want to know that, is there any other way to use my code one time and code will run when document ready and  event occur. I have also another question that is it possible to run code when any changes come into document. Here is my code.
    $( document ).ready(function() {

        var main_content = $(".main_content").outerHeight();
        var sidebar = $(".sidebar").outerHeight();
        if (main_content>sidebar) {
            var extra = main_content-sidebar;
            var new_sidebar = sidebar+extra;
            $(".sidebar").css('min-height', new_sidebar);
        };

        $(document).on('click', '.add', function(event) {
            var main_content = $(".main_content").outerHeight();
            var sidebar = $(".sidebar").outerHeight();
            if (main_content>sidebar) {
                var extra = main_content-sidebar;
                var new_sidebar = sidebar+extra;
                $(".sidebar").css('min-height', new_sidebar);
            };
        });

    });