title: Unbind existing function on Bootstrap Modal Close
body: <p>The website that I am working on has quite a few modal popups. </p>
  
  <p>Each popup has about 50 fields/inputs, images and other custom elements in the body. For this reason a simple form reset <code>$("#formId")[0].reset()</code> won't work (Because it won't remove the images and reset the custom elements that was created by another developer).</p>
  
  <p><strong>The theory behind my solution.</strong></p>
  
  <p>Basically I made a function that will reload the modal popup from the include file that it loads from initially every time that the modal gets closed. It gets the URL of the file from a attribute added to each modal popup called <code>data-url</code>.</p>
  
  <p>The function then gets the code for the modal popup and replaces it with the new code. That way the modal popup is empty again, as if it was never been opened.</p>
  
  <p>The following code is to reset the modal. This is run initially when the document loads in the <code>register_website_components()</code> function, binding the function onto all modals on the page.
  It then uses a function called <code>modal_close_trigger</code> to re-add it self to the new modal code.</p>
  
  <p>The issue comes that if you have closed the modal, it reloads it twice. </p>
  
  <p>IE: In chrome developer tools I can see 2 individual requests loading the same code for the same modal popup.</p>
  
  <p>I want to <code>unbind</code> the previous close function and re-add it.</p>
  
  <pre><code>function reset_modal(modal_id, modal_url) {
      $.ajax({
          type: "GET",
          url: modal_url,
          data: "",
          contentType: false,
          cache: false,
          processData: false,
          beforeSend: function(){
              $("[data-toggle=modal][data-target="+modal_id+"]").attr("disabled","disabled");
              $(modal_id + " .modal-body").html(loading_bar());
              $(modal_id + " .modal-footer").html("");
          },
          success: function (data) {
              $(modal_id).replaceWith(data);
              // This is the function that I want to unbind.
              $(modal_id).on("hidden.bs.modal", function () {
                  var modal_parent = $(modal_id).parent();
                  $(modal_id).remove();
                  $(modal_parent).append(data);
                  register_website_components();
                  modal_close_trigger(modal_id, modal_url);
              });
              $(modal_id).modal("hide");
              $("[data-toggle=modal][data-target="+modal_id+"]").removeAttr("disabled");
          }
      });
  }
  </code></pre>
  
  <p><code>loading_bar()</code> is simply a function to load a Bootstrap Progress bar.</p>
  
  <pre><code>function loading_bar() {
      $process_bar_id = guid();
      var $process_bar = "&lt;div class='col-lg-12' id='" + $process_bar_id + "'&gt;";
      $process_bar = $process_bar + "&lt;div class='progress'&gt;";
      $process_bar = $process_bar + "&lt;div class='progress-bar progress-bar-success progress-bar-striped active process-bar' role='progressbar' aria-valuenow='100' aria-valuemin='0' aria-valuemax='100' style='width:100%'&gt;&lt;/div&gt;";
      $process_bar = $process_bar + "&lt;/div&gt;";
      $process_bar = $process_bar + "&lt;/div&gt;";
      $process_bar = $process_bar + "&lt;/div&gt;";
      return $process_bar;
  }
  </code></pre>
  
  <p><code>modal_close_trigger()</code> is just code to reset the modal trigger but the code can be found below.</p>
  
  <pre><code>function modal_close_trigger(modalId, modalURL) {
      $(modalId).on("hidden.bs.modal", function () {
          reset_modal(modalId, modalURL);
      });
  }
  </code></pre>
  
  <p><code>register_website_components()</code> is a function that runs initially when the page is ready to run scripts. This function binds all sorts of other functions to various components on the website.</p>
  
  <p>In conventional jQuery you have several options to <code>unbind</code> an existing function from an element. Examples can be <a href="http://api.jquery.com/unbind/" rel="nofollow">found here (jQuery API Documentation)</a>.</p>
  
  <p>I looked the function below but I have no idea how to bind that to the modal close event.</p>
  
  <pre><code>var handler = function() {
    alert( "The quick brown fox jumps over the lazy dog." );
  };
  $( "#foo" ).bind( "click", handler );
  $( "#foo" ).unbind( "click", handler );
  </code></pre>
  
  <p>The modal close code came <a href="http://getbootstrap.com/2.3.2/javascript.html#modals" rel="nofollow">from here (Bootstrap API Documentation)</a>.</p>
  
  <p>What I need to know if how do I <code>unbind</code> this code: <code>$(modal_id).on("hidden.bs.modal")</code> or how do I use the example above to <code>bind</code>/<code>unbind</code> a function using a handler for the modal popup.</p>