In the first, I create a block. The second is to remove it. However, for some reason it does not delete, although in other cases everything works. Those. when I make append with loading the page via php, everything is ok. And if I add using ajax, then the deletion is no longer applied to it. How can I fix it?

$(document).ready(function(){ $('#sub').click(function() { $.ajax({ url: "../register/writes/wall_support.php", type: "POST", dataType: "text", data: {write: $('#textarea').val(), button: $('#sub').val()}, success: function(data) { var arr = data.split('^'); var app = '<div value="'+arr[3]+'" class="read"><img src="../img/support.png" align="left"> <div class="united"><span style="color: #3333FF;">Поддержка <button class="deletewrite" value="'+arr[3]+'" title="Удалить"></button></span><br/><div style="width: 95%;">'+arr[0]+'</div><span style="color: #515151; font-size: 0.8em; font-family: Arial, sans-serif;">Опубликовал '+arr[1]+' администратор '+arr[2]+'</span></div></div>'; $(app).insertAfter("#after"); $('#textarea').val(''); $('#allwrites').text('Всего '+arr[4]+' записей'); } }) }); $('.deletewrite').click(function() { var value = $(this).val(); $.ajax({ url: "../register/writes/wall_support.php", type: "POST", dataType: "text", data: {number: value}, success: function(data) { $(this).parents('.read').remove(); $('#allwrites').text('Всего'+data+' записей'); } }) }); }); 

    2 answers 2

    You load the element with the deletewrite class via an AJAX, but you forget to attach an event handler to it, so nothing happens using live or delegate , for example:

     $('.deletewrite').live('click',function() { 
    • Thank you very much! Everything is working! - Svyatoslav
    • Since the jQuery version 1.7, .live () method is obsolete. $ (". deletewrite"). on ("click", function () {...}); - iKuzko

    In general, the question caption does not correspond to what you need ... Perhaps you made a mistake here:

      dataType: "text", 

    You tell the PHP script what you expect from it is plain text, not HTML. Here it is to you and "answers" in plain text, which can not be processed by the browser, like HTML and / or JS! Replace this error with this:

      dataType: "html", 

    Should help ...


    It would also be nice to add the function "error" to the jQuery Ajax query that is being created, on par with "success". This will help in eliminating errors, if the text message of an error is alert in this function.

    • No that's not the point. - Svyatoslav