0

I have a site in which content is dynamic loaded via jQuery .load(). Sadly, I can't catch an event from the dynamic loaded content even though using .on().

This is my function in which I can't trigger the $("#control_right").on('click')-function after loading changeContent(2):

function changeContent(step){
$('#content').toggle(100);
setTimeout(function() {
      $('#content').load("template/step_" + step + ".html");
}, 180);
$('#content').delay(300).toggle(200).queue(function(){

    if(step === 1){ // IF STEP 1. CHOOSE MALE OR FEMALE
        console.log("step1");
        $("#choice_man").on('click', function(){
            changeContent(2);
        });

        $("#choice_woman").on('click', function(){
            changeContent(2);
        });
    }

    if(step === 2){ // IF STEP 2. Do something else
        $("#control_right").on('click', function() {
            console.log("CLICK");
        });
    }
});
}

I hope my problem is understandable.

2 Answers 2

2

Try changing selector to one from DOM elements:

$(document).on('click', '#control_right', function(){...})

Bdw, why are you using .delay(300).toggle(200)?

.load('url.html #loadContentFrom', function(){/* Callback function */})
Sign up to request clarification or add additional context in comments.

Comments

1

like many other answer in stackoverflow (please search before posting your question), you have to use on delegated event and not just on().

 $('#content').on('click', "#choice_man",function(){
       ...

delegating it to your closest static parent present in the document at the time of insertion is preferred rather than document itself performancewise

Comments

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.