.dialog() state persistence with ajax enabled site

.dialog() state persistence with ajax enabled site

Hello everybody.

I have a site under development that uses AJAX to retrieve a list of items.  This list is formatted into divs that contain basic info and then has a details button (that opens a dialog).  These results are paginated on the server (one of the parameters for the AJAX request is number of results, another is offset for SQL query). 

With this all working, I have a problem where when you click the "next" button to retrieve the next n entries from the database, the correct results are returned for the basic info.  However, when you click the details button, the details of the original results are shown.

I have tried using .dialog("destroy") to clean out the dialogs, but then I can't open any dialogs on subsequent pages. 

It seems that there's persistence once the dialog object is instantiated.  This seems to be corroborated by this article: http://blog.nemikor.com/2009/04/08/basic-usage-of-the-jquery-ui-dialog/.  How can I remove this persistence on future AJAX requests so that my dialogs will be working properly on all pages?

Here's some code:

  1. [...  this is all inside a for loop, looping through XML elements - variables have been assigned...]
  2. $("#hclResults").append(
  3.   '<div id="hclResult-' + a + '" class="hclResultContainer ui-corner-all">'
  4.             [... some HTML to show basic info ...]
  5.    +'<div class="hclDetailsButtonContainer"><button id="hclDetailsButton-' + a + '" class="hclDetailsButton" value="#hclDetailsDialog-' + a +'">Details</button></div>'
  6.    +'</div>'
  7.    +'<div id="hclDetailsDialog-' + a + '" class="hclDetailsDialog" title="Details"></div>'
  8. );

  9. $("#hclDetailsDialog-" + a).append( 
  10.               [... HTML showing details ...]
  11.    +'<div id="oemDetail"></div>'
  12. );
  13. if (oem) {
  14.    $("#hclDetailsDialog-" + a + " > div#oemDetail").append( 
  15.               [... HTML showing details ...]
  16.    );
  17. }

  18. $("#hclDetailsDialog-" + a).dialog({
  19.    autoOpen: false
  20. });

  21. $("#hclDetailsButton-" + a).button({
  22.    icons: {
  23.       primary: 'ui-icon-newwin'
  24.    }
  25. }).click(function(){
  26.    var dialogSelector = $(this).attr("value");
  27.    $(dialogSelector).dialog('open'); 
  28.    return false;
  29. });
  30.            
  31. a++;

PS - I know the HTML here in unseemly, but it's just a mockup.  I'm more concerned with the JS functionality.  If my redaction of the code removes too much, please let me know.