Hello i am trying to write javascript not showing some contents when there are specific keywords in dom. But i dont want to display none for the keywords showing in a
element inside those classes. For example
if ($("div[class*='artist']").length || $("div[class*='designer']").length) {
jQuery("div[class*='artist'], div[class*='designer']").each(function () {
if (jQuery(this).text().toLowerCase().indexOf("firstword") !== -1 || jQuery(this).text().toLowerCase().indexOf("secondword") !== -1) {
jQuery(this).hide();
console.log(this);
}
});
}
So i know that this code works very good but i have problem because hiding me more things that i want.
You can exclude the divs with p
tag using find
function as follows:
if ($("div[class*='artist']").length || $("div[class*='designer']").length) {
jQuery("div[class*='artist'], div[class*='designer']").each(function () {
var hasPTag = $(this).find('p');
if ((jQuery(this).text().toLowerCase().indexOf("firstword") !== -1 || jQuery(this).text().toLowerCase().indexOf("secondword") !== -1) && hasPTag.length == 0) {
jQuery(this).hide();
console.log(this);
}
});
}
I hope this helps.