S
S
Shamil Khairulaev2021-04-24 12:17:54
AJAX
Shamil Khairulaev, 2021-04-24 12:17:54

Where did I make a mistake in pagination via ajax?

I have a script in which pagination via ajax should be triggered when a button is clicked.
On the first press it works correctly, but on subsequent presses it does not work correctly.

function posts_pagination() {
   const paginationBtn = $('.more-btn');

   paginationBtn.each(function () {
      $(this).click(function (e) {
         alert('Работает');
         e.preventDefault();
         const clicked = $(this);
         let page = clicked.children('span').text();
         const limit = 5;
         const offset = limit * (page - 1);
         $.ajax({
            type: 'POST',
            url: 'php-scripts/posts.php',
            dataType: 'html',
            data: {
               page: ++page
            },
            success: function (data) {
               const main = $('.main');
               clicked.remove();
               main.append(data).append('<div class="more"><a href="#" class="more-btn">Далее >> <span style="display: none;"> ' + (page) + ' </span>');
            }
         });
      });
   })

}

In the first appendix to the main, I add posts, in the second, a button that causes pagination.
Please tell me where is the error. I will answer all clarifying questions. Thanks in advance.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question