jQuery: There's a more efficient way of doing this, isn't there?
<p>I have a plugin which essentially sorts a table on the server side and puts the content back into a container. I have a binding function which adds a click event to the table header cells to call the parent form submission. I feel like this isn't the best way of going about this. Any ideas?</p>

<pre><code>$.fn.myplugin = function() {
  return this.each(function() {
    var parentform = $(this).parents("form");
    var tableidentifier = $(this).attr("id");

    var bindclicks = function() {
      parentform.find("table#" + tableidentifier + " thead tr th").click(function() {
        // Some code to change the sort column- boring!
        parentform.submit();
      });
    }

    bindclicks();

    parentform.submit(function() {
      $.post(parentform.attr("action"), parentform.serialize(), function(res) {
        parentform.find("table#" + tableidentifier).replaceWith($(res));
        bindclicks();
      })
      return false;
    });
  });
}
</code></pre>

<p>I call the <code>bindclicks()</code> function first to set the click handlers up, then since I am doing <code>replaceWith()</code> I am calling it again to re-bind those events. It works, but I'm curious..</p>
