title: Rerun JavaScript on successful ajax call
body: <p>I have a delete button with the remote: true option set: </p>
  
  <pre><code># _categories.html.erb
  &lt;%= link_to 'Destroy', category, method: :delete, data: { confirm: 'Are you sure?' }, remote: true %&gt;
  </code></pre>
  
  <p>My destroy method uses json:</p>
  
  <pre><code># categories_controller.rb
    def destroy
      @category = Admin::Category.find(params[:id])
      @category.destroy
      save_to_history("The category \"#{@category.name}\" has been destroyed", current_user.id)
  
      respond_to do |format|
        # You can't retrieve the @categories before attempting to delete one. 
        @categories = Admin::Category.all
  
        format.json
      end
    end
  </code></pre>
  
  <p>and my destroy.json.erb file looks like:</p>
  
  <pre><code>#destroy.json.erb
  &lt;% self.formats = ["html"] %&gt;
  {
    "html":"&lt;%= raw escape_javascript( render :partial =&gt; 'categories', :content_type =&gt; 'text/html') %&gt;"
  }
  </code></pre>
  
  <p>Now my problem is that I have this JavaScript run on page load, and deleting an initial category works as intended... Until the data changes. I need this bellow JavaScript to run again every time the user changes data either by adding a new category or deleting a category. How? Please be kind with your help, I do NOT know JavaScript at all! haha</p>
  
  <pre><code>  &lt;script type='text/javascript'&gt;
  
      $(function(){
        /* delete category */
        $('a[data-remote]').on('ajax:success', function(event, data, status, xhr){
          $("#dashboard_categories").html(data.html);
        });
      });
  
    &lt;/script&gt;
  </code></pre>
  
  <p>FULL index.html.erb:</p>
  
  <pre><code># index.html.erb
  &lt;% title "Categories" %&gt;
  
  &lt;%= form_for @category, remote: true do |f| %&gt;
    &lt;% if @category.errors.any? %&gt;
      &lt;div id="error_explanation"&gt;
        &lt;h2&gt;&lt;%= pluralize(@category.errors.count, "error") %&gt; prohibited this category from being saved:&lt;/h2&gt;
  
        &lt;ul&gt;
        &lt;% @category.errors.full_messages.each do |msg| %&gt;
          &lt;li&gt;&lt;%= msg %&gt;&lt;/li&gt;
        &lt;% end %&gt;
        &lt;/ul&gt;
      &lt;/div&gt;
    &lt;% end %&gt;
  
    &lt;table&gt;
      &lt;tr&gt;
        &lt;td&gt;&lt;%= f.text_field :name, placeholder: 'Category Name' %&gt;&lt;/td&gt;
        &lt;td&gt;&lt;%= f.text_field :color, placeholder: 'Colour' %&gt;&lt;/td&gt;
        &lt;td&gt;&lt;%= f.submit %&gt;&lt;/td&gt;
      &lt;/tr&gt;
    &lt;/table&gt;
    &lt;br /&gt;
  &lt;% end %&gt;
  
  
  &lt;div id="dashboard_categories"&gt;
    &lt;%= render partial: 'categories' %&gt;
  &lt;/div&gt;
  
  
  &lt;% content_for :javascript do %&gt;
    &lt;script type='text/javascript'&gt;
  
      $(function(){
  
        /* new category */
        $('#new_admin_category').on('ajax:success', function(event, data, status, xhr){
          $("#dashboard_categories").html(data.html);
        });
  
        /* delete category */
        $('a[data-remote]').on('ajax:success', function(event, data, status, xhr){
          $("#dashboard_categories").html(data.html);
        });
      });
  
    &lt;/script&gt;
  &lt;% end %&gt;
  </code></pre>