How to make a like in news feed and correct the problem: js does not "see" loaded via ajax blocks?

Hello, there is a website, it is published.

The principle is very similar to "In touch": when scrolling to the bottom of the page need to be loaded news.

Every piece of news the user can write a comment, put like, seen yet

Now it's all done very crooked.
The basic requirement is ajax that all these actions are performed without reloading the page.

Now the huskies made thus:
Using Php generated id of elements. Over the counter.
1. Like1 button that sends the value of the hidden field id_pub1 in the ajax handler.
2. Like2 button, it sends the value of the hidden field id_pub2 in the ajax handler.
And so to all records.

In JS it is handled like this:
$(document).ready(function () {
 var i = 0; 
 while(i < 1000){
(function(i){
$("#like"+i).click(function(){funcLike(i)});
$("#dislaik"+i).click(function(){funcDisLike(i)});
})(i++);
}
 });


Respectively in the functions is already checked took it like/disli or set, is sent to the handler, etc.

1 question: is it at all organized? It confuses the counter in js to 1000, because, theoretically, you can download and more publications. And indeed the generation of id-shnik in php, feel in the end will lead to some confusion.

The next problem is that if I dogrusu using ajax publishing, the above js code does not handle downloaded id. The problem is common, but the solutions I found.

2 question: Again, originally is it right at all? If Yes, then how to get around this problem?

Well, anyway, the main purpose of my question is to know how would You approach implementing this type of issue?

Thanks in advance for your answers.
September 19th 19 at 12:28
3 answers
September 19th 19 at 12:30
Solution
html
<div class="news">
 some news1
 <button class="like" data-id="1">like</button>
</div>

<div class="news">
 some news2
 <button class="like" data-id="2">like</button>
</div>


jquery
$(document).on('click','.like',function(){
 id = $(this).attr('data-id');

 // your function
funcLike(id);
});
Thanks for the reply, really helped) When reloading via ajax works too - lou.Cremin commented on September 19th 19 at 12:33
September 19th 19 at 12:32
Strange, Vanille such problems are not observed, use it instead of ikiru, the more it is faster than ikiru
Thanks for the reply. There's not a jQuery thing, but me more) of the answers below have done and earned. - lou.Cremin commented on September 19th 19 at 12:35
September 19th 19 at 12:34
All these things are stored in a database(DB), means you are sending ajax data to a file that will handle everything you need(php), and it must be added the necessary entry to the database. In the database you store who put a like (can't put it 100 times), what a post(e.g. post id) and so on.... The counter can be updated every 1-10 seconds via ajax. SQL-em, you can count the number of records on the huskies in a DB for post. Do not use setInterval() it uses a hidden call to eval(), which undermines the security of the code.
Thanks for the reply. Of course everything is stored in mysql, I have a question regarding the organization of js/ajax/html in php and mysql there are like more or less everything :) - lou.Cremin commented on September 19th 19 at 12:37

Find more questions by tags JavaScriptHTMLjQueryAJAXCSS