Good day
This is not how I can not figure out how to properly organize the code, namely, to work with events
There is a normal block
<div id="content"> тут разные теги </div> This block is periodically added / replaced with html code.
Roughly speaking, it is possible to say something like a site that works without refreshing the page, respectively, the js code is loaded only once when you first enter the site
In general, the essence of the problem. Suppose in this block id = "content" added messages "chat"
<div id="content"> <div id="wrapChat"> <div class="message"> <span class="remove">Удалить</span> Тут сообщение </div> <div class="messgae"> <span class="remove">Удалить</span> Тут сообщение </div> </div> </div> Or there goods
<div id="content"> <div class="item"> Тут товар 1 </div> <div class="item"> Тут товар 2 </div> </div> The question is how to properly hang events on dynamic content?
Of course, the first thing that this “event delegation” thought was, but there is one big BUT
A lot of different content is loaded into the block, it turns out that for each button you need to hang an event through id = "content" of the type:
$(document).ready(function () { var content = $("#content"); content.on("click", "#wrapChat .remove", function (e) { // тут чо-то важное }); content.on("mouseenter", "#wrapChat .message", function (e) { // тут чо-то важное }); content.on("click", ".item", function (e) { // тут чо-то важное }); // ... // ... // ... // ... // ... // ... }); And what if the forum uploads there, deleting messages, adding, quoting, answering, there are a lot of different buttons on which you need to broadcast events
It seems to me somehow wrong, or does it seem to me? )
I would be grateful for any help)
For me, this is all a wonder since I am more on the server part of the "walk"
PS Because of my "cockroaches" I can not hang the event on a straight line in html type
<div class="item" onclick="">