I have an item class, with data-id="integer" , how to make an alert using jquery, if there are 2 or more blocks on the page with the same number in the data-id ? I need it for debag.

    1 answer 1

    You can solve this:

     $(document).ready(function() { var ids = $('.item').map(function(){ return $(this).attr('data-id'); }).get(); if (ids.length) { var id_counts = {}; $(ids).each(function() { id_counts[this] = id_counts[this] + 1 || 1; }); $.each(id_counts, function(key, value) { if (value > 1) { alert('.item with data-id=' + key + ' duplicates ' + value + ' times.'); } }); } }); 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="item" data-id="1"></div> <div class="item" data-id="2"></div> <div class="item" data-id="3"></div> <div class="item" data-id="4"></div> <div class="item" data-id="2"></div> <div class="item" data-id="4"></div> 

    Or look at the code on jsfiddle

    • one
      I only suspect that the mutating one should also be checked - DOMNodeInserted - Artem Gorlachev