Search code examples
jqueryajaxgetheightafter-effects

Hello! How can get the height for an element after ajax pass the data to the element: $("#contents").html(data) $("#contents").height()?


I have search on stackoverflow and i have found many answers for that but all is wrong so. I now i need to pass first the data to the element and then call the height function, but is still not working after the data has html on the element.

$.ajax({
    url: el.attr("data-action"),
    dataType: "html",
    type: "GET",
    contentType: "html",
    success: function(data) {
        var id = el.attr('data-modal');

        $("body").append("<div id='" + id + "' class='modal'><div class='modal-contents'>" + data + "</div></div>");

        var modal = $("#" + id);

        contents = modal.find(".modal-contents").first();

        alert(contents.height());

        modal.addClass("visible");

        contents.slideDown("slow");

    }
});

Solution

  • You need to use .ajaxComplete() to be sure the Element is complete loaded(sorry for bad english)

    try this:

    $( document ).ajaxComplete(function( event, request, settings ) {
        var modal = $('#YOUR_ID'),
        contents = modal.find(".modal-contents").first();
        alert(contents.height());
    });