Search code examples
jqueryclone

jquery to run clone function with multiple selectors


$(document).ready(function() {
    $(".add_file_group, .clone_email2, .clone_email").click(function() {
        $("#clone_file_group").clone().insertAfter("div#clone_file_group:last");
        console.log(this);
    });
});

Using this function with different selectors in one section, just one selector is working


Solution

  • As your class name clone_email2, .clone_email suggest that these are generated dynamically. So you need to use delegate event handler

    Try this

     $(document).on('click', '.add_file_group, .clone_email2, .clone_email', function () { 
    
           $("#clone_file_group").clone().insertAfter("div#clone_file_group:last");
    
        });
    

    Description: Attach a handler to one or more events for all elements that match the selector, now or in the future, based on a specific set of root elements.