Detect if two elements have the same identifier on the page

When loading a page from the database, 5 elements are selected. When the user scrolls down, another 5 messages are loaded, etc.

To prevent duplicate messages when sorting by 'new', I put page load time in hidden input, which will be used in JavaScript:

<input id="loadTime" type="hidden" value="<?php echo time(); ?>">

Then, when you select more messages while scrolling, I only select new ones until this time to prevent duplicate messages.

WHERE s.date < :loadTime

However, my problem is sorting by hot or top because it does not use the same logic.

Is it possible to detect that two elements on a page have the same identifier? Because when a duplicate post is added to the page, it will have the same identifier as on it. In some form, $(window).scroll()is there a function that removes a duplicate ID after that, so that the user does not see duplicate messages?

+4
source share
3 answers

I was able to solve it:

In the success function, after adding new messages, I look through all the identifiers on the page (taken from this, and then delete the last element with this identifier.

$('[id]').each(function(){
    var ids = $('[id="'+this.id+'"]');
        if (ids.length>1 && ids[0]==this){
            $("#"+this.id).last().remove();
        }
 });
+1
source

, JavaScript-, , 5?

( ) "" "" , , , , , 5 , .

0

â–ē , ?

. id.

DEMO

if($('[id="same"]').length > 1){
  alert("duplicate id")
}

// To remove last inserted element with duplicate ID
$('[id="same"]').last().remove()
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="same">
<input type="text" id="same">
<input type="text" id="same">
<input type="text" id="same" value="Last element">
Hide result
0

All Articles