Cannot get click () function to work with jQuery

Thank you in advance:

This function works great when dropping a div from the first click function, but SHOULD NOT confirm the second click function in any capacity.

I can even activate the first click function as many times as I want.

What am I missing? I will tear my hair into it.

$('span[rel="confirm"]').click( function() {
    $('.confirmbox').remove();

    targetpath = $(this).attr("targetpath");
    dbid = $(this).attr("dbid");

    $(this).after('<div><span class="closeout">X</span> &nbsp Are you sure you want to <a href="index.php?cmd=deletesample&id=' + dbid + '&filetarget=' + targetpath + '">delete?</a></div>');
    $('.confirmbox').show(200);
});

$('.closeout').click( function() {
    $('.confirmbox').css('background-color', 'green');
});
+3
source share
2 answers

You add the element dynamically, so you need to use $ .live () instead:

$('.closeout').live("click", function(){
    $('.confirmbox').css('background-color', 'green');
});
+7
source

Since you are dealing with dynamic DOM elements, you need to change your click () to the live () event instead ...

$('.closeout').live('click', function() {
    $('.confirmbox').css('background-color', 'green');
});

live() http://api.jquery.com/live/

+5

Source: https://habr.com/ru/post/1732120/


All Articles