.remove(); does not work in html tags added with jquery

With Jquery I added filds on page after selecting images in my computer or in manager images of site.

And display all images selecteds in div #show:


$("#show").append("<div class='remove_this_image col-md-3'><span class='dashicons dashicons-no' style='float: right;'></span><img src=" +attachment.url+" class='img-responsive' /><input type='hidden'  name='my_image_URL[]' value=" +attachment.url+"></div>");

But for exclude each image I need click on icon dashicons-no

        $('.dashicons-no').click ( function(e){
                e.preventDefault();
                $(this).closest('.remove_this_image').remove();
        });

If the image is already loaded in the html delete button this works, but if the image was added to the html with jquery the delete button does not work

I appreciate your help

Use delegated events. http://api.jquery.com/on/#direct-and-delegated-events

Thanks

Still not working, I tried like this:

$('.remove_this_image .dashicons').click ( function(e){
       e.preventDefault();
       $(this).closest('.remove_this_image').remove();
 });

that’s not a delegated event … did you read the link?

1 Like

Sorry,

$(document).on( 'click', '.dashicons', function(e){

This worked for me.

Thank you again

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.