The bottom line is this: you need to do something similar not to the wall of the VC. Along the way, the following problem arose. After the user has entered the message text and clicked "send", an ajax request is sent, after successful execution, again by an Ajax, this message is loaded and embedded in the DOM . There is also a link to delete the message. So, those messages that were added immediately now, without reloading the page, cannot be deleted. Only those that were loaded at the time of loading the page are deleted. Tell me, please, where to dig. If you need a code, put it here.

    4 answers 4

    See what kind of situation it turns out: when loading a page, you put necessary actions on existing DOM objects. But after loading, you simply add objects to the DOM, and the scripts don’t know that you changed something! Thus, when adding new items, you need to manually attach events to them - that's all. Faced with this personally, so I know what I'm talking about.

    • Yes. Thank. I have come to this long ago. Maybe prompt how in js event'y are hung? - Vitaly Kustov
    • I hung them immediately after adding DOM elements, wrote a function and called it after adding. Those. in ajax () there was something like the following: success: function (data) {$ ("# someDiv"). append (data); initActions (); } // The function itself: function initActions () {$ (". Close"). Click (function () {...}); } Basically the same thing as @SilverIce - metazet advised you

    Most likely, you have the appointment of an event when you click on the delete somewhere once. Well, something like $('.comments .delbutton').click(...

    And when you add ext. the object, then it has the necessary structure, but EVENT is not hung. That is, after .append'a, you need to assign an event (http://api.jquery.com/delegate/) or, which is probably simpler, to add a banal < onClick="" >

      If jQuery is used then it would be logical to use the live function http://api.jquery.com/live/

      PS: I personally, when I work without jQuery, describe a function like bindDelete, etc. in which, for example, I hang up onclick or some other events in the cycle, which are not really the essence, the point is that writing it not in a cycle but with checking the existence of an event binding to an element, then you can simply call this function when added.
      PPS: why the site does not check for emptiness when adding a message? O_o

        Strange, real time, how are the records updated? Do - delete objects from the DOM. After deletion - a separate request for deletion from the database.

        • Yes, I do. Only the link which js-th is added doesn’t react at all to the click event - Vitaly Kustov
        • How not to react? How do you add a new comment to the DOM? - V_Tjuryakin
        • A new message is added to the DOM dynamically. That it is not removed. The rest, which are loaded with the page, are deleted regularly. - Vitaly Kustov
        • I understand - throw off the code as you add to the house. - V_Tjuryakin
        • I process the delLink.click () event; inside it, if everything is ok, then container.prepend (data); data - html code of the new message. container is the block where masquegs are inserted: var container = $ ("# messaages"); In general, I solved the problem by simply registering onclick in html. But I still want to figure out how to remove dynamically added elements and in the way I failed - Vitaly Kustov