There are several identical divs on the site, and they are always different, how to find a duplicate in each diva and hide it? What would be left for one of the types of diva. They differ in the value of the data-name

    1 answer 1

    I will assume that the elements being processed are not nested in each other, and we must leave the first of each group. This can be done so (but you should check the browser support for the features used):

     document.querySelector("button").addEventListener('click', function (event) { var used = Object.create(null) var elems = document.querySelectorAll("[data-name]") for (var q=0; q<elems.length; ++q) { var name = elems[q].dataset.name if (used[name]) { elems[q].remove() } else { used[name] = true } } }) 
     <button>Remove</button> <div data-name=a>a-1</div> <div data-name=b>b-1</div> <div data-name=a>a-2</div> <div data-name=a>a-3</div> <div data-name=b>b-2</div> <div data-name=b>b-3</div> <div data-name=a>a-4</div> <div data-name=c>c-1</div> 

    • Thank you, you understood me correctly, they helped a lot! - SloGS
    • on your example, for some reason it works, and he hides all my divas for some reason, leaves no one - SloGS
    • And everything, I figured out, it was necessary to vyrat all the divas in the class I needed, thank you very much again, everything works - SloGS