Question

I can't figure out why the jQuery doesn't fire the second time I click the button. I'm clueless, if you need more code or anything else, just ask.

HAML:

The detail container which contains the button (%input.newRemark.newEntity.tm_button{:type => "button", :value => "New"}):

.detail_header_container.remark
.red_title.detail_header.open#explore_management-configurationremarks_header="Remarks"
%input.newRemark.newEntity.tm_button{:type => "button", :value => "New"}
%div.listingContainer            
  .entityDetailsContainer      
    %table.configDetails{:class => "configuration-#{entity.id}"}
      %thead
        %tr
          %th{:style => "width:25%"} Date
          %th{:style => "width:65%"} Remark
          %th{:style => "width:10%h"} Actions
      %tbody
        - ConfigurationRemark.where(:access_line_id => entity.access_line_id).sort(:created_at).each do |remark|       
          = render(:haml, :"explore_management/configurationremark_record", :locals => {:entity => remark})

The modal that jQuery won't fire a second time:

-# ModalContent
%div{:id => "add_remark_modal"}
  %h1 Add remark to Access ID: #{entity.access_line_id}
  %div.formErrors
  %form.remarkForm#explore_management-configurationremarks_form{:action => "explore_management/configurationremarks",  :method => "POST"}
    %input{:type => "hidden", :name => "_method", :value => "POST"}
    %input{:type => "hidden", :name => "access_line_id", :value => entity.access_line_id, :name => "entity[access_line_id]"}
    %input{:type => "hidden", :name => "isFirst", :value => isFirst, :name => "config[isFirst]"}
    %input{:type => "hidden", :name => "isLatest", :value => isLatest, :name => "config[isLatest]"}
    %input{:type => "hidden", :name => "entity_index", :value => entity_index, :name => "config[entity_index]"}
    %input{:type => "hidden", :name => "entity_array", :value => entity_array, :name => "config[entity_array]"}
    %div
      %label{:for => "remark"} Remark:
      %input{:id => "remark", :type => "text", :name => "entity[remark]"}

    %div#entityFormSubmit
      %input.tm_button.add_remark{:type => "submit", :value => "Save"}

jQuery:

// Click-event for dynamically added new-button(s) 
$(document).on('click', '.newRemark', function(event){
    // Open Remark popup
    $("#add_remark_modal").modal();

    // Set remark form save functions
    onRemarkFormLoadSuccess();      
});

function onRemarkFormLoadSuccess(data) {
    var method = $(".remarkForm").find('input[name="_method"]').val();
    var remarkFormOptions = { dataType: "html", 
                              method: "POST",
                              success: onRemarkSaveSuccess,
                              error: onRemarkSaveError                          
                            };
    $(".remarkForm").ajaxForm(remarkFormOptions);   
}

function onRemarkSaveSuccess(record_html, statusText, jqXhr, jqForm) {
    //remove form
    $("#simplemodal-overlay").remove();
    $("#simplemodal-container").remove();

    var headerId = jqForm.attr('id').replace('_form', '_header');
    var $listingContainer = $("#" + headerId).parent().find(".listingContainer");
    var $tableBody = $("#" + headerId).parent().find("table.configDetails tbody");
    //append new record
    $tableBody.find("tr.nothingFound").remove();
    var $newRecord = $(record_html).appendTo($tableBody);

    //fade in$

    $newRecord.hide();
    $newRecord.css('background-color', '#FFFF66');
    $newRecord.fadeIn(1000, function() { $newRecord.css('background-color', 'inherit');});

    registerRecordActionHandlers($newRecord);

}

function onRemarkSaveError() {

    alert("error");

}

function registerRecordActionHandlers($record) {
        $record.click(onRecordClick);
        $record.find(".recordActions a").click(onRecordActionClick);
}

VISUAL:

Some visual for better understanding:

remarks


add_remark

Was it helpful?

Solution

I've added the following line to my onClick() handler. It closes the remaining modals that are open. The first time I click there isn't a modal open but it's doesn't give an error so fine by me.

$.modal.close();

The handler:

// Click-event for dynamically added new-button(s) 
$(document).on('click', '.newRemark', function(event){

    //Close 
    $.modal.close();

    // Open Remark popup
    $("#add_remark_modal").modal();

    // Set remark form save functions
    onRemarkFormLoadSuccess();      
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top