jQuery: Get reference to click event and trigger it later?
<p>I want to wrap an existing click event in some extra code.</p>

<p>Basically I have a multi part form in an accordion and I want to trigger validation on the accordion header click. The accordion code is used elsewhere and I don't want to change it.</p>

<p>Here's what I've tried:</p>

<pre><code>   //Take the click events off the accordion elements and wrap them to trigger validation
    $('.accordion h1').each(function (index, value) {
        var currentAccordion = $(value);
        //Get reference to original click
        var originalClick = currentAccordion.click;

        //unbind original click
        currentAccordion.unbind('click');

        //bind new event           
        currentAccordion.click(function () {
            //Trigger validation
            if ($('#aspnetForm').valid()) {
                current = parseInt($(this).next().find('.calculate-step').attr('data-step'));             
                //Call original click.
                originalClick();
            }
        });
    });
</code></pre>

<p>jQuery throws an error because it's trying to do <code>this.trigger</code> inside the <code>originalClick</code> function and I don't think <code>this</code> is what jQuery expects it to be.</p>

<p><strong>EDIT: Updated code. This works but it is a bit ugly!</strong></p>

<pre><code>   //Take the click events off the accordion elements and wrap them to trigger validation
    $('.accordion h1').each(function (index, value) {
        var currentAccordion = $(value);
        var originalClick = currentAccordion.data("events")['click'][0].handler;
        currentAccordion.unbind('click');
        currentAccordion.click(function (e) {
            if ($('#aspnetForm').valid()) {
                current = parseInt($(this).next().find('.calculate-step').attr('data-step'));
                $.proxy(originalClick, currentAccordion)(e);
            }
        });
    });
</code></pre>
