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?
many many thanks in advance
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>
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";
}
});
});
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.