title: jquery functions within modal only work on first open, after close and re-open they stop working
body: <p>I am using jquery modal, within each modal I have a form. I am then loading certain jquery functions onto the form elements, for example jquery autocomplete.</p>
  
  <p>When I open the modal for the first time it is working fine with all the jquery functions within the script, but as soon as I close the first modal and open again none of the functions work.</p>
  
  <p>Its as though they are binded on the page load to the form, then when the button is clicked for the modal its works and then closing the modal unbinds it. </p>
  
  <p>Does anybody have any ideas as to why they would stop working after the first modal open/close?</p>
  
  <pre><code>$(document).ready(dialogForms);
  function dialogForms() {    
   $('a.menubutton').click(function() {
      var a = $(this);
      $.get(a.attr('href'),function(resp){
        var dialog = $('&lt;div&gt;').attr('id','formDialog').html($(resp).find('form:first').parent('div').html());
        $('body').append(dialog);
        dialog.find(':submit').hide();
        dialog.dialog({
          title: a.attr('title') ? a.attr('title') : '',
          modal: true,
          buttons: {
            'Save': function() {
  
                  if($(this).find('form').valid()){
                  // do stuff if form validates
                  submitFormWithAjax($(this).find('form'));               
                  $('#homepage').trigger('click');    
                  $(this).dialog('close');
                  } 
                  else {
  
                  }             
                  },
            'Cancel': function() {$(this).dialog('close');}
          },
          width: 650,
          height: 400,        
          show: "fade",
          hide: "fade"
        });
  
  $('#edit_vle').bind('change', function (e) { 
      if( $('#edit_vle').val() == 'FE') {
        $('#fe_automcomplete1').show();
        $('#he_automcomplete1').hide();
        $("#edit_he_title").val("");
        $("#edit_he_code").val("");
      }
      else{
        $('#fe_automcomplete1').hide();
        $('#he_automcomplete1').show();
        $("#edit_fe_title").val("");
        $("#edit_fe_code").val("");
      }         
  });
  
  
  $('#delete_vle').bind('change', function (e) { 
      if( $('#delete_vle').val() == 'FE') {
        $('#fe_automcomplete2').show();
        $('#he_automcomplete2').hide();
        $("#delete_he_title").val("");
        $("#delete_he_code").val("");
      }
      else{
        $('#fe_automcomplete2').hide();
        $('#he_automcomplete2').show();
        $("#delete_fe_title").val("");
        $("#delete_fe_code").val("");
      }         
  });
  
  
      var epronames = [&lt;?php 
      $eprotmp = Array();
      while($eprorow = mssql_fetch_array($epro_course)) $eprotmp[] = 
      '{
                  title: "'.$eprorow['Name'].'",
                  label: "'.$eprorow['Code'].' - '.$eprorow['Name'].'",
                  code: "'.$eprorow['Code'].'",
                  user: "'.$eprorow['fname'].' '.$eprorow['sname'].'",
      }'; 
      echo join(',', $eprotmp);
  ?&gt;];
  
      var fenames = [&lt;?php 
      $fetmp = Array();
      while($ferow = mysql_fetch_array($feinactive)) $fetmp[] = 
      '{
                  title: "'.$ferow['course'].'",
                  label: "'.$ferow['shortname'].' - '.$ferow['course'].'",
                  code: "'.$ferow['shortname'].'",
      }'; 
      echo join(',', $fetmp);
  ?&gt;];
  
      var henames = [&lt;?php 
      $hetmp = Array();
      while($herow = mysql_fetch_array($heinactive)) $hetmp[] = 
      '{
                  title: "'.$herow['course'].'",
                  label: "'.$herow['shortname'].' - '.$herow['course'].'",
                  code: "'.$herow['shortname'].'",
      }'; 
      echo join(',', $hetmp);
  ?&gt;];
  
          $("#title").autocomplete({
              minLength: 3,
              source: epronames,
              focus: function( event, ui ) {
                  $("#title").val( ui.item.label );
                  return false;
              },
              select: function( event, ui ) {
                  $("#title").val( ui.item.title );
                  $("#code").val( ui.item.code );
                  $("#ctl").val( ui.item.user );
                  return false;
              },
              change: function(event, ui) {
              if (!ui.item) {
                  $("#title").val("");
              }
              }
          })
          .data( "autocomplete" )._renderItem = function( ul, item ) {
              return $( "&lt;li&gt;&lt;/li&gt;" )
                  .data( "item.autocomplete", item )
                  .append( "&lt;a&gt;" + item.label + "&lt;/a&gt;" )
                  .appendTo( ul );
          };
  
  
  
          $("#edit_he_title").autocomplete({
              minLength: 3,
              source: henames,
              focus: function( event, ui ) {
                  $("#edit_he_title").val( ui.item.label );
                  return false;
              },
              select: function( event, ui ) {
                  $("#edit_he_title").val( ui.item.title );
                  $("#edit_he_code").val( ui.item.code );
                  return false;
              },
              change: function(event, ui) {
              if (!ui.item) {
                  $("#edit_he_title").val("");
              }
              }
          })
          .data( "autocomplete" )._renderItem = function( ul, item ) {
              return $( "&lt;li&gt;&lt;/li&gt;" )
                  .data( "item.autocomplete", item )
                  .append( "&lt;a&gt;" + item.label + "&lt;/a&gt;" )
                  .appendTo( ul );
          };
  
  
  
          $("#edit_fe_title").autocomplete({
              minLength: 3,
              source: fenames,
              focus: function( event, ui ) {
                  $("#edit_fe_title").val( ui.item.label );
                  return false;
              },
              select: function( event, ui ) {
                  $("#edit_fe_title").val( ui.item.title );
                  $("#edit_fe_code").val( ui.item.code );
                  return false;
              },
              change: function(event, ui) {
              if (!ui.item) {
                  $("#edit_fe_title").val("");
              }
              }
          })
          .data( "autocomplete" )._renderItem = function( ul, item ) {
              return $( "&lt;li&gt;&lt;/li&gt;" )
                  .data( "item.autocomplete", item )
                  .append( "&lt;a&gt;" + item.label + "&lt;/a&gt;" )
                  .appendTo( ul );
          };      
  
  
  
  
  
  
  if($("#clearform").length &gt; 0){
  $("#clearform").click(function() {
      $("#title").val('');
      $("#code").val('');
      $("#ctl").val('');
  });
  }
  
  if($("#delete_fe_clearform").length &gt; 0){
  $("#delete_fe_clearform").click(function() {
      $("#delete_fe_title").val('');
      $("#delete_fe_code").val('');
  });
  }
  if($("#edit_fe_clearform").length &gt; 0){
  $("#edit_fe_clearform").click(function() {
      $("#edit_fe_title").val('');
      $("#edit_fe_code").val('');
  });
  }
  
  if($("#delete_he_clearform").length &gt; 0){
  $("#delete_he_clearform").click(function() {
      $("#delete_he_title").val('');
      $("#delete_he_code").val('');
  });
  }
  if($("#edit_he_clearform").length &gt; 0){
  $("#edit_he_clearform").click(function() {
      $("#edit_he_title").val('');
      $("#edit_he_code").val('');
  });
  }
  
  
  }, 'html');
  return false;
  });
  
  }
  
  function submitFormWithAjax(form) {
    form = $(form);
    $.ajax({
      url: form.attr('action'),
      data: form.serialize(),
      type: (form.attr('method')),
      dataType: 'script',
      success: function(data){
     }
    });
    return false;
  }
  </code></pre>
  
  <p>Here are the buttons for each modal.</p>
  
  <pre><code>&lt;a href="new_course.php" class="menubutton" id="new_course" title="New Course"&gt;New Course&lt;/a&gt;
  &lt;a href="edit_course.php" class="menubutton" id="edit_course" title="Edit Course"&gt;Edit Course&lt;/a&gt;
  &lt;a href="delete_course.php" class="menubutton" id="delete_course" title="Delete Course"&gt;Delete Course&lt;/a&gt;
  </code></pre>