views:

441

answers:

2

I'm loading content via an AJAX call into a table row added to the DOM on the fly. I'm calling the datepicker functionality in my callback function and the calendar shows fine. However, when I click on the date, I get an error: inst is null. Here's my code:

$(document).ready(function() {
    $(".edit").live("click", function() {
        //Delete previously loaded edit row
        $("#tempEditRow").remove();

        //Get the record id of the row to be edited
        var recordID = $(this).parent("td").parent("tr").attr("id");

        //Add the new row to the document
        $(this).parent("td").parent("tr").after("<tr id=\"tempEditRow\"><td id=\"tempEditCell\" colspan=\"100\"></td></tr>")

        //Get a reference to the new row
        var container = $("#tempEditCell");

        //Populate the container
        populateContainer("/wpm/includes/ajax_editApplication.cfm?id=" + recordID, container);
    });
});

function populateContainer(ajaxUrl, container) {
    //Populate the container
    $.ajax({
        url: ajaxUrl,
        cache: false,
        success: function(html){
            $(container).html(html);
            $('.datepicker').datepicker();
        }
    }); 
}

I've tried deleting the hasDatepicker class, deleting any references to the datepicker, etc. but nothing is working. Thanks in advance for helping!

A: 

Try calling datepicker('destroy') as part of wiping out the previous row (do it before your remove() call).

$("#tempEditRow .datepicker").datepicker('destroy');
$("#tempEditRow").remove();
James Koch
Thanks James. The requirements changed and the date field came out of the layout, so I can't confirm that this will resolve the issue. Hopefully this helps someone else though.
Robert
+1  A: 

I had the same error, which was because I had two datepickers input fields with the same id, one setup during runtime and the other through ajax. Gave them a unique id and it all worked

andy-score
This was the issue... I had duplicate IDs in the page, same situation with one loaded at runtime, the other through ajax.
Robert
glad you got it sorted
andy-score