0

My jQuery .click functions are not working on content that is loaded with ajax. I changed my .click to .on ("click") functions because I read that event delegation would fix the problem. Here is my ajax call:

    $(".piece:nth-child(1)").click(function() {
        $.ajax({
            url: "terra.html",
            datatype: "html",
            async: false,
            success: function(data) {
                $("body").fadeOut(function() {
                    $(this).html(data).slideDown(500);
                });
            }
        });
    });

And this is the function that is not working after the ajax call:

$(document).on('click', ".pin", function() {
    $(this).css("z-index", "99");
    $(".dropdown-container").animate({"top": "0px"}, 500, "ease");
    $(".terra-arrow-dropdown").show();
    $(".interact .terra-arrow").fadeOut(300);
    $(".interact .screen").addClass("screen-opacity");
});
12
  • Is the element with the .pin class on the page when it is loaded? Commented Nov 15, 2015 at 0:19
  • @user2182349 it is on the page being loaded with Ajax, which is titled terra.html. It is not on the index.html page Commented Nov 15, 2015 at 0:35
  • @AustinBranham What are contents of terra.html ? Commented Nov 15, 2015 at 1:06
  • @guest271314 Its part of a web design portfolio. The entire document includes a number of things, interactive divs, images etc Commented Nov 15, 2015 at 2:48
  • @guest271314 what would be the best way of sending you the contents of terra.html? Commented Nov 15, 2015 at 2:48

2 Answers 2

1

Does returned html include <!doctype> declration ? , <html> element ?

yes it includes all of that.

Appear body element is overwritten at

$("body").fadeOut(function() {
  $(this).html(data).slideDown(500);
});

Try removing <!doctype> declaration , <html> , <body> tags from terra.html .

If <script> tags are in body element , move script elements to head of original document , for script where

$(document).on('click', ".pin", function() {

not to be overwritten when

$(this).html(data)

called , which replaces all html of body element - including any child script elements

Sign up to request clarification or add additional context in comments.

1 Comment

Actually, removing the easing parameters in my animate functions did the trick. Would it be proper to also go ahead and fix all of this as well?
0

Check your console.log for errors. My guess is that your ajax callback function is failing because jQuery's fadeOut's first parameter is the duration and the second is the callback function. So you would need something like this.

$(".piece:nth-child(1)").click(function() {
    $.ajax({
        url: "terra.html",
        datatype: "html",
        async: false,
        success: function(data) {
            $("body").fadeOut(500, function() {
                $(this).html(data).slideDown(500);
            });
        }
    });
});

The way you are doing event delegation ( $(document).on('click', ".pin", function() {... ) should work.

1 Comment

This unfortunately did not fix it. I've included some info in the comment section above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.