0

This is pretty much the same problem as in here: Click event doesn't work on dynamically generated elements , but that post was a few years old, and it's solution did not work for me.

I have a small image carousel, which works properly when I add it directly to html, but when I add it dynamically the jQuery click event for buttons does not work. I tried changing from button.click() to button.on("click,function(){}) but it still doesn't work. Can someone tell me a) How to make it work b) How does modyfying DOM affects jQuery execution, so I understand it better in the future (for example, are there some DOM element jquery ignores?)

my code:

$(document).ready(function(){
    $(".carousel-button-left").on('click',function(){
    var $ul=$(this).next().find(".carouselUl");;
    //alert(""+$ul.html());
    var $lastchild=$ul.children().last();
    $ul.prepend($lastchild);
    });
});


$(document).ready(function(){
    $(".carousel-button-right").on("click",function(){
    var $ul=$(this).prev().find(".carouselUl");
    //alert(""+$ul.html());
    var $firstchild=$ul.children().first();
    $ul.append($firstchild);
    });
});
1

3 Answers 3

3

try changing:

$(".carousel-button-left").on('click',function(){
...

to

$(document).on('click', '.carousel-button-left', function(){
...

so that jQuery receives the event and delegates it the element that matches the selector provided as argument.

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

Comments

1

use event delegation for registering handlers for dynamically added elements.

Just using .on() does not make the event registration delegation based, the parameter and the element to which the handler is attached is important

$(document).ready(function () {
    $(document).on('click', ".carousel-button-left", function () {
        var $ul = $(this).next().find(".carouselUl");;
        //alert(""+$ul.html());
        var $lastchild = $ul.children().last();
        $ul.prepend($lastchild);
    });
});

$(document).ready(function () {
    $(document).on("click", ".carousel-button-right", function () {
        var $ul = $(this).prev().find(".carouselUl");
        //alert(""+$ul.html());
        var $firstchild = $ul.children().first();
        $ul.append($firstchild);
    });
});

the set of elements on which the handler is added(LHS of .on() call) should be present in the dom when the script is executed, then we need to use a second parameter in the .on() call to specify the target element selector which are present under the LHS elements.

1 Comment

It worked. Could you plese explain why previous options did not work?
0

try

$(document).ready(function(){
    $(document).on('click',".carousel-button-left",function(){
    var $ul=$(this).next().find(".carouselUl");;
    //alert(""+$ul.html());
    var $lastchild=$ul.children().last();
    $ul.prepend($lastchild);
    });
});


$(document).ready(function(){
    $(document).on("click",".carousel-button-right",function(){
    var $ul=$(this).prev().find(".carouselUl");
    //alert(""+$ul.html());
    var $firstchild=$ul.children().first();
    $ul.append($firstchild);
    });
});

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.