How to remove any elements that DO NOT have a hyphen in it

Viewed 114

I have a tag list that has mostly tags that start with a hyphen. I want to remove or hide all tags that DO NOT have this character. Because of limited access to code, it has to be done with jQuery/javascript. My attempt doesn't work, how to fix?

Here is my HTML:

<div class="widget Label" data-version="2" id="Label1">
  <h3 class="title">
    Tags
  </h3>
  <div class="widget-content cloud-label-widget-content">
    <span class="label-size label-size-5">
      <a class="label-name" href="https://about.xarcell.studio/search/label/art">
        art
        <span class="label-count">
          10
        </span>
      </a>
    </span>
    <span class="label-size label-size-5">
      <a class="label-name" href="https://about.xarcell.studio/search/label/-gallery">
        -gallery
        <span class="label-count">
          9
        </span>
      </a>
    </span>
    <span class="label-size label-size-5">
      <a class="label-name" href="https://about.xarcell.studio/search/label/-photography">
        -photography
        <span class="label-count">
          9
        </span>
      </a>
    </span>
    <span class="label-size label-size-5">
      <a class="label-name" href="https://about.xarcell.studio/search/label/-portfolio">
        -portfolio
        <span class="label-count">
          9
        </span>
      </a>
    </span>
    <span class="label-size label-size-5">
      <a class="label-name" href="https://about.xarcell.studio/search/label/opinion">
        opinion
        <span class="label-count">
          9
        </span>
      </a>
    </span>
  </div>
</div>

Javascript:

$('#label1').each(function() {
    $('#label1 span').find(':not(:contains("-"))').remove();
});
5 Answers

jQuery

jQuery(function($) {
  $(`.label-size:not(:contains("-"))`).remove();
});

Vanilla JavaScript

document.addEventListener("DOMContentLoaded", function(){
  for(let node of document.getElementsByClassName("label-size")) {
    if(!/-/.test(node.textContent)) {
      node.remove();    
    }
  }
});
$('.label-size').each((element) => { 
element.textContent.indexOf('-') >= 0 ? element.remove() : null;
});

Something like that should work. You need to check each label-size element, so looping over a unique identifier like an id won't work.

Follows a full working example, using vanilla javascript:

// Gets the dom element which id="Label1"
const labelDiv = document.getElementById("Label1");

// Gets all elements which has the css class name 'label-name'
const labelNameElements = labelDiv.getElementsByClassName("label-name");

// For each element 'label-name' dom element
for(let i=0; i < labelNameElements.length; i++) {
     
     // Verifies if the element contains a '-'
     if(labelNameElements[i].text.indexOf('-') > -1) {
        
        //removes the parent element (in this case the <span class="label-size label-size-...">
        labelNameElements[i].parentElement.remove();
        
        //removes 1 to the index since a element was removed from the labelNameElements array.
        i--;
     }     
}
<div class="widget Label" data-version="2" id="Label1">
  <h3 class="title">
    Tags
  </h3>
  <div class="widget-content cloud-label-widget-content">
    <span class="label-size label-size-5">
      <a class="label-name" href="https://about.xarcell.studio/search/label/art">
        art
        <span class="label-count">
          10
        </span>
      </a>
    </span>
    <span class="label-size label-size-5">
      <a class="label-name" href="https://about.xarcell.studio/search/label/-gallery">
        -gallery
        <span class="label-count">
          9
        </span>
      </a>
    </span>
    <span class="label-size label-size-5">
      <a class="label-name" href="https://about.xarcell.studio/search/label/-photography">
        -photography
        <span class="label-count">
          9
        </span>
      </a>
    </span>
    <span class="label-size label-size-5">
      <a class="label-name" href="https://about.xarcell.studio/search/label/-portfolio">
        -portfolio
        <span class="label-count">
          9
        </span>
      </a>
    </span>
    <span class="label-size label-size-5">
      <a class="label-name" href="https://about.xarcell.studio/search/label/opinion">
        opinion
        <span class="label-count">
          9
        </span>
      </a>
    </span>
  </div>
</div>

This answer works with jQuery:

$('.label-size').each(function() {

    $(this).find(':not(:contains("-"))').remove();
    
});
.label-name {
  display:block; /* Optional, adds space between links*/
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="widget Label" data-version="2" id="Label1">
  <h3 class="title">
    Tags
  </h3>
  <div class="widget-content cloud-label-widget-content">
    <span class="label-size label-size-5">
      <a class="label-name" href="https://about.xarcell.studio/search/label/art">
        art
        <span class="label-count">
          10
        </span>
      </a>
    </span>
    <span class="label-size label-size-5">
      <a class="label-name" href="https://about.xarcell.studio/search/label/-gallery">
        -gallery
        <span class="label-count">
          9
        </span>
      </a>
    </span>
    <span class="label-size label-size-5">
      <a class="label-name" href="https://about.xarcell.studio/search/label/-photography">
        -photography
        <span class="label-count">
          9
        </span>
      </a>
    </span>
    <span class="label-size label-size-5">
      <a class="label-name" href="https://about.xarcell.studio/search/label/-portfolio">
        -portfolio
        <span class="label-count">
          9
        </span>
      </a>
    </span>
    <span class="label-size label-size-5">
      <a class="label-name" href="https://about.xarcell.studio/search/label/opinion">
        opinion
        <span class="label-count">
          9
        </span>
      </a>
    </span>
  </div>
</div>

The problem was that the code wasn't using its own element inside the .each function, and it wasn't using the correct element.

Also, if you want a one liner, there's a much easier way to do this:

$(.label-size:not(:contains("-"))).remove();

This is a jQuery solution, if you want a vanilla JS try @Ricardo Rocha solution:

$(document).ready(function() {
  const tags = $(".label-name");
  tags.each(function(index, element) {
    element.text.trim().startsWith("-") ? '' : element.remove();
  });
});
.label-name {
  display: block
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="widget Label" data-version="2" id="Label1">
  <h3 class="title">
    Tags
  </h3>
  <div class="widget-content cloud-label-widget-content">
    <span class="label-size label-size-5">
      <a class="label-name" href="https://about.xarcell.studio/search/label/art">
        art
        <span class="label-count">
          10
        </span>
    </a>
    </span>
    <span class="label-size label-size-5">
      <a class="label-name" href="https://about.xarcell.studio/search/label/-gallery">
        -gallery
        <span class="label-count">
          9
        </span>
    </a>
    </span>
    <span class="label-size label-size-5">
      <a class="label-name" href="https://about.xarcell.studio/search/label/-photography">
        -photography
        <span class="label-count">
          9
        </span>
    </a>
    </span>
    <span class="label-size label-size-5">
      <a class="label-name" href="https://about.xarcell.studio/search/label/-portfolio">
        -portfolio
        <span class="label-count">
          9
        </span>
    </a>
    </span>
    <span class="label-size label-size-5">
      <a class="label-name" href="https://about.xarcell.studio/search/label/opinion">
        opinion
        <span class="label-count">
          9
        </span>
    </a>
    </span>
  </div>
</div>

Related