How to wrap / nest javascript calls that use 'this'?
<p>I am writing a function which accepts a <code>Button</code> object and should set <code>onclick</code> client script which conditionally calls the <em>existing</em> client script. For example, if I start with a button which would render like:</p>

<pre><code>&lt;input type="button" value="ok" id="ok"
  onclick="existingClickHandler(this);"
  /&gt;
</code></pre>

<p>I want to programmatically set <code>OnClientClick</code> on this button so it would render something like:</p>

<pre><code>&lt;input type="button" value="ok" id="ok"
  onclick="Wrapper(function { existingClickHandler(this); });"
  /&gt;
</code></pre>

<p>The problem is that the existing code uses <code>this</code>, I don't know how to write <code>Wrapper()</code> in such a way that <code>this</code> still points to the correct object.</p>

<p>How can I make the <code>existingClickHandler</code> continue to operate properly when called by <code>Wrapper()</code>?</p>

<hr>

<p>Update: Per request, the scenario is I am writing an ASP.NET function to disable buttons on click, in order to prevent double-posts, and for some UI responsiveness. In my server-side C# code I have a function (this has been condensed a bit for clarity): </p>

<pre><code>void AutoDisableButtons(Button sourceButton, params Button[] buttonsToDisable)
{
    string oldScript = sourceButton.OnClientClick;
    if (string.IsNullOrEmpty(oldScript))
        oldScript = "null";
    else
        oldScript = "function(){" + oldScript + ";}";
    postbackEventScript = ....
    buttonsToDisableArrayScript = ....
    sourceButton.OnClientClick = "return DisableButtonsOnSubmit.call(this, " + oldScript + ", " + postbackEventScript + ", " + buttonsToDisableArrayScript + ");";
}
</code></pre>

<p>And the corresponding client script:</p>

<pre><code>function DisableButtonsOnSubmit(oldClickScript, postbackEventReference, buttonsToDisableArray)
{
    if((typeof(Page_ClientValidate) == 'function') &amp;&amp; !Page_ClientValidate())
        return false;
    if((typeof(oldClickScript) == 'function') &amp;&amp; !oldClickScript.call(this))
        return false;
    for (var i=0; i&lt;buttonsToDisableArray.length; i++)
    {
        if(null != document.getElementById(buttonsToDisableArray[i]))
        {
            document.getElementById(buttonsToDisableArray[i]).disabled = true;
        }
    }
    postbackEventReference.call(this);
    return true;
}
</code></pre>

<p>Note that this code includes the <code>.call()</code> solution proposed by @herostwist</p>
