0

How can I hide an element if it contains a child has a class which has already occurred?

So all containers that have a child with the class "child*" which has already occurred would be hidden

So in this example, the second and fifth containers would be hidden. I know their class will start with child" but I don't know what the remainder of class will be.

<div class="container">
  <div class="child.65">
  </div>
</div>

 <div class="container">
  <div class="child.65">
  </div>
</div>

 <div class="container">
  <div class="child.11">
  </div>
</div>

<div class="container">
  <div class="child.47">
  </div>
</div>

<div class="container">
  <div class="child.11">
  </div>
</div>

To summarize, how do I hide a container a duplicate class of which I only know the first string of characters?

Sam
  • 5,150
  • 4
  • 30
  • 51

3 Answers3

1

To make this work you can loop through each .container > div element, then select all the other elements that share the same class, and remove all but the first of them. Something like this:

$('.container > div').each(function() {
  $('.' + this.className.replace('.', '\\.')).not(':first').parent().hide();
}); 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
  <div class="child.65">first 65</div>
</div>
<div class="container">
  <div class="child.65">second 65</div>
</div>
<div class="container">
  <div class="child.11">first 11</div>
</div>
<div class="container">
  <div class="child.47">first 47</div>
</div>
<div class="container">
  <div class="child.11">second 11</div>
</div>

Note that the logic could be made much cleaner and faster through the use of data attributes on the .container elements themselves to define the groups - assuming you are able to amend the existing HTML structure.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
0

There are many ways to do it in JS or JQuery. Here is the pure javascript solution.

var divs = document.getElementsByTagName("div");
var child_list = [];
for(var i=0; i<divs.length; i++){
  if(divs[i].className.indexOf("child")>-1){
    var className = divs[i].className;
    var number = parseInt(className.substr(className.indexOf(".")+1, className.length));
    if(child_list.indexOf(number)>-1){
      divs[i].style.display="none";
    } else {
      child_list.push(number);
    }
  }
}
<div class="container">
  <div class="child.65">
  1. child.65
  </div>
</div>

 <div class="container">
  <div class="child.65">
    2. Child.65
  </div>
</div>

 <div class="container">
  <div class="child.11">
  3. Child.11
  </div>
</div>

<div class="container">
  <div class="child.47">
  4. Child.47
  </div>
</div>

<div class="container">
  <div class="child.11">
  5. child.11
  </div>
</div>
Abdullah Danyal
  • 1,106
  • 1
  • 9
  • 25
0

There are many ways to achieve the same, this comes from the top of my head:

var arr = [];
$(".container").each(function(){
    $(this).children("div[class^='child']").each(function){
      if(arr.indexOf($(this).attr("class"))>(-1)){
         $(this).hide();
      }
      else{
         arr.push($(this).attr("class"));
      }
    });
});
DIEGO CARRASCAL
  • 1,999
  • 14
  • 16
  • Does this take in to account only children with the class "child*" ? – Sam Jul 19 '17 at 16:15
  • @Sam now it does, div with class starting by "child" [like in this answer](https://stackoverflow.com/questions/2178416/using-starts-with-selector-on-individual-class-names) – DIEGO CARRASCAL Jul 19 '17 at 17:09