On page 10 are div with the item class.
When you click on this div, a .remove() jquery occurs and the element is deleted via AJAX.
Now after applying remove I need to calculate how much div.item data is on the page, through the code inspector I counted 9, but the script counts 1 more, first 10 (but there are 9), then 9 (but 8).

 $('.item').click(function () { var divNumberOnPage = $('.item').length; console.log(divNumberOnPage); var setCurrentPagination = $('.number').html(divNumberOnPage); return setCurrentPagination; }); 

Closed due to the fact that off-topic participants Grundy , tutankhamun , Denis , aleksandr barakin , Streletz 4 Oct '16 at 10:16 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - Grundy, tutankhamun, Denis, aleksandr barakin, Streletz
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • four
    The given code considers true. either little code or something else affects the result - lexxl
  • 3
    And where in the code When you click on this div, the .remove () jquery occurs and the element is deleted via AJAX. ? I can assume that the number is displayed before deletion, so it seems that there was an error - Grundy

2 answers 2

and the element is removed via AJAX

What is it like?

No elements are deleted via AJAX - requests occur through ajax. Perhaps the error is that you delete an element in the callback of the AJAX ( complete: function(r){..} or success: function(r){..} or .done(function(r){..}) ) : after making a request. And consider the amount outside the callback, for example after calling jQuery.ajax .

In this case, an error in understanding, since the ajax-answer comes later (and the callback is executed), than the next line is jQuery.ajax after jQuery.ajax .

And the counting algorithm is correct.

    Because it is necessary to consider in the same processor as delete. Otherwise, you send a request, recount the elements - there are still 10 of them, then you process the answer and delete one.