title: Any way to pass parameters programmatically to an onclick function?
body: <p>I have an <code>onclick</code> function which performs several tasks.  In another JavaScript function I do not have access to the context variables needed to perform these tasks.  To get around this I have been simply calling the <code>onclick</code> function directly.</p>
  <p>The problem I have now is that I'd like to perform a task after an Ajax action in the <code>onclick</code> completes.  Is there any way for me to pass a function to the <code>onclick</code> method of a link?  What would the <code>onclick</code> attribute look like?</p>
  <p>e.g. something like this:</p>
  <pre><code>&lt;a id=&quot;link3&quot; href=&quot;javascript:void(0);&quot; onclick=&quot;function(callback) { X(a); Y(b); Z(c, callback); };&quot;&gt;click me&lt;/a&gt;
  </code></pre>
  <p>Clicking on this would pass &quot;undefined&quot; as the callback, while I could also call it explicitly like this:</p>
  <pre><code>document.getElementById(&quot;link3&quot;).onclick(function() { alert(&quot;Completed all tasks&quot;); } );
  </code></pre>
  <p>Is something like this possible?  Basically I want to be able to pass an optional parameter to the <code>onclick</code> method, but if it's absent I want it to behave as if there were just procedural code in the <code>onclick</code>.</p>
  <p><strong>EDIT: Added more specifics</strong></p>
  <p>The use case is that the <code>onclick</code> calls a search function which requires session variables.  When regular searches are performed there is no problem, but when I wish to refresh the search results (e.g. when a the user edits or deletes one of the returned results) I need to call the same search function that was called previously and then print out a feedback message after they are refreshed.  This feedback message output is what I would like to be in the callback method.  I'd also like to set the <code>window.location.href</code> to page down to the affected row in the event of an edit or an insert.</p>
  <p>I have access to several variables on the server side which I print out directly to the <code>onclick</code> attribute when I render the page.  In other JavaScript files that I am including on my page I would also like to be able to execute the same functions contained within the <code>onclick</code>.  The only problem being that I would have to pass all these variables to those functions indirectly through several intermediate methods in the call chain.</p>
  <p>The other option is to print these values to hidden page values and then retrieve them from within my method, but I would like to avoid this as it would be cluttering up everything just so that I could decorate my function call with some visual after-effects.</p>