0

I am trying to hide a div if another div contains a specific word.

This is my HTML

<div class="dynamic">
  This text is hidden if another div contains the word "download"
</div>
<div class="something">
  <div class="btn-download">
  If this text has the word "download" the div with class "dynmaic" is hidden
  </div>
</div>

JS

jQuery(document).ready(function($){
if ( $('.btn-download').text() === 'download' ) {
  $('.dynamic').hide();
}
});

What am I doing wrong? And do I need jQuery for it?

JSFiddle

many many thanks in advance

SilberArt
  • 5
  • 6
  • 1
    You're not doing anything wrong, your btn-download text simply doesn't == "herunterladen" https://jsfiddle.net/9btwhk1o/ – freedomn-m Mar 25 '22 at 09:48
  • *do I need jQuery for it?* - no, you don't *need* jquery get the text / check its value / hide an element. – freedomn-m Mar 25 '22 at 09:53

3 Answers3

0

If a text in div contain a specific word you can use includes method on string ($(selector).text().includes('your-text-search'))

if ( $('.btn-download').text().includes('download') ) {
  $('.dynamic').hide();
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="dynamic">
  This text is hidden if another div contains the word "download"
</div>
<div class="something">
  <div class="btn-download">
  If this text has the word "download" the div with class "dynmaic" is hidden
  </div>
</div>
jeremy-denis
  • 6,368
  • 3
  • 18
  • 35
0

This can help, I'm not an expert but try to edit.

$(document).ready(function() {
  $("#dynamic").click(function() {
    var res = $('#dynamic').text();
    if (res == "value") {
      var hide = document.getElementById("something");
      hide.style.display = "none";
    }
  });
});
freedomn-m
  • 27,664
  • 8
  • 35
  • 57
Kashyap Kumar
  • 13
  • 1
  • 4
0

Your example code needs jQuery. This is a solution using ECMAScript 5 or older and jQuery.

jQuery(document).ready(function($){
  if ( $('.btn-download').text().indexOf('download') !== -1) {
    $('.dynamic').hide();
  }
});

Please check the fiddle https://jsfiddle.net/1sx39q6h/

For jQuery + ECMAScript 6, you have the solution of @jeremy-denis.

Valeriu Ciuca
  • 2,084
  • 11
  • 14